LearnFrenzy
Sign in Take a free test

Operators · Predict the Output · Question 2

i++ and ++i in one expression Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
let i = 1;
const r = i++ + ++i;
console.log(i, r);
  • A 3 3
  • B 3 4 Correct
  • C 2 4
  • D 3 5
Correct Answer: Option B

Left to right: i++ gives 1 and makes i 2; ++i makes i 3 and gives 3. r = 1 + 3 = 4.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links