LearnFrenzy
Sign in Take a free test

Operators · Predict the Output · Question 15

% with negative and decimal numbers Answer, explanation & discussion

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

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

main.js
console.log(-7 % 3, 7 % -3, 5.5 % 2);
  • A 2 -2 1.5
  • B -1 1 1.5 Correct
  • C -1 -1 1
  • D 1 1 1.5
Correct Answer: Option B

JavaScript's % is a remainder, not a mathematical modulo: its sign follows the dividend. It also works on decimals: 5.5 = 2 × 2 + 1.5.

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