Given the code let num = 10; const increaseNumber = () => num++; const num1 = increaseNumber(); console.log(num1); What will be the output of num1?

Prepare for the Salesforce JavaScript Developer I Certification Exam. Utilize interactive quizzes, flashcards, and detailed explanations for each question. Boost your confidence and ace your exam effortlessly!

In this code, the initial value of the variable num is set to 10. The function increaseNumber is defined as an arrow function that increments the value of num by 1 each time it is called, effectively returning the old value before incrementing.

When const num1 = increaseNumber(); is executed, the function increaseNumber is called. At this moment, it does the following:

  1. It evaluates the current value of num, which is 10.

  2. It then proceeds to execute the increment operation (num++), which first returns the current value of num (10) and then increments num to 11.

The value returned by the function, which is now assigned to num1, is the original value of num before the increment occurred. Therefore, when the value of num1 is logged in the console, it displays 10.

This behavior of the post-increment operator (num++) is crucial in understanding why the output is 10. The increment happens after the original value is returned, leading to this particular output. Thus, the output of num1 is 10.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy