here is my code.
JavaScript
x
6
1
let userName = 'dave'
2
userName = userName ? console.log(`Hello, ${userName}`) : console.log('Hello!')
3
4
let userQuestion = 'Am I crazy'
5
console.log(`${userName} asked: ${userQuestion}`)
6
expected output
JavaScript
1
3
1
Hello, dave
2
dave asked: Am I crazy
3
actual output
JavaScript
1
3
1
Hello, dave
2
undefined asked: Am I crazy
3
Why is it showing ‘undefined` ?
Advertisement
Answer
you are trying to assign the console.log to userName and using it. change the code to
JavaScript
1
5
1
let userName = 'dave'
2
console.log(userName ? `Hello, ${userName}` : 'Hello!');
3
4
let userQuestion = 'Am I crazy'
5
console.log(`${userName} asked: ${userQuestion}`)
or you can use the line as follows.
JavaScript
1
2
1
userName ? console.log(`Hello, ${userName}`) : console.log('Hello!')
2
JavaScript
1
6
1
let userName = 'dave'
2
userName ? console.log(`Hello, ${userName}`) : console.log('Hello!');
3
4
5
let userQuestion = 'Am I crazy'
6
console.log(`${userName} asked: ${userQuestion}`)