0 votes
in JavaScript by

What is the output?

function checkAge(age) {
  if (age < 18) {
    const message = "Sorry, you're too young.";
  } else {
    const message = "Yay! You're old enough!";
  }

  return message;
}

console.log(checkAge(21));
  • A: "Sorry, you're too young."
  • B: "Yay! You're old enough!"
  • C: ReferenceError
  • D: undefined

1 Answer

0 votes
by

Answer: C

Variables with the const and let keyword are block-scoped. A block is anything between curly brackets ({ }). In this case, the curly brackets of the if/else statements. You cannot reference a variable outside of the block it's declared in, a ReferenceError gets thrown.

Related questions

0 votes
asked Mar 12 in JavaScript by DavidAnderson
0 votes
asked Feb 26 in JavaScript by DavidAnderson
...