Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


What will be the output of the following code: console.log(x); var x = 10;

  1. 10

  2. undefined

  3. ReferenceError

  4. NaN

The correct answer is: undefined

In the provided code snippet, the output of `console.log(x); var x = 10;` will indeed be `undefined`. This behavior is a result of JavaScript's variable hoisting mechanism. When the JavaScript engine processes the code, it first hoists the declaration of the variable `x`, which means it acknowledges that `x` exists before it reaches the line where it is logged to the console. However, the assignment of `10` to `x` occurs only after the `console.log` call. So, at the time of the `console.log` execution, `x` has been declared but not yet initialized, leading it to have the value `undefined`. To summarize, JavaScript hoisting allows variable declarations to be lifted to the top of their containing function or scope, so even though `var x = 10;` appears later, `x` is recognized as a declared variable when `console.log(x);` is executed, resulting in an output of `undefined`.