0 votes
in JavaScript by
What is the output?
function sayHi(name) {
  return `Hi there, ${name}`;
}

console.log(sayHi());
  • A: Hi there,
  • B: Hi there, undefined
  • C: Hi there, null
  • D: ReferenceError

1 Answer

0 votes
by

Answer: B

By default, arguments have the value of undefined, unless a value has been passed to the function. In this case, we didn't pass a value for the name argument. name is equal to undefined which gets logged.

In ES6, we can overwrite this default undefined value with default parameters. For example:

function sayHi(name = "Lydia") { ... }

In this case, if we didn't pass a value or if we passed undefinedname would always be equal to the string Lydia

Related questions

0 votes
asked Mar 13 in JavaScript by DavidAnderson
0 votes
asked Mar 11 in JavaScript by DavidAnderson
...