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 console.log(number++) and console.log(++number) output for the variable defined as let number = 0?

  1. 1 1 2

  2. 1 2 2

  3. 0 2 2

  4. 0 1 2

The correct answer is: 0 2 2

The output for the expressions `console.log(number++)` and `console.log(++number)` when the variable `number` is defined as `let number = 0` will be `0` for the first log and `2` for the second. Initially, `number` is set to `0`. When the expression `console.log(number++)` is executed, the current value of `number` (which is `0`) is logged to the console. The post-increment operator (`number++`) increases the value of `number` by `1` after the current value has been returned. Therefore, `0` is printed to the console, and afterward, `number` becomes `1`. Next, when `console.log(++number)` is executed, the pre-increment operator (`++number`) increases the value of `number` by `1` before it's logged. Since `number` was `1` after the previous operation, it becomes `2` with this pre-increment operation, and then `2` is printed to the console. Thus, the final sequence of outputs is `0` (from the post-increment) followed by `2` (from the pre-increment), which fits the output from the