Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
JavaScript Console.log Duplicate printing

Here is the code I put into node.js:

let x = 4;
console.log(x);
x = 5; 

I got this in the console:

4
5

Any idea why re-assigning a value to x is causing another call to console.log()? I'm in a class involving JS and the instructor isn't quite sure either

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Both in the browser console and in the Node repl, after typing in a statement and pressing enter, the "completion value" of the statement you typed in will be printed. With expressions, usually, this is equivalent to what the expression evaluates to. And, in JavaScript, assignments are (unfortunately) expressions - so x = 5 evaluates to 5:

console.log(x = 5);

So when you type the line

x = 5; 

and press enter, that completion value - 5 - gets printed.

This behavior does not occur when running files (like node myscript.js, or <script src="myscript.js">) - it's purely a console quirk. If it confuses you, consider only using the console for only playing around with very short toy snippets - for real code, put it into a file, then execute the file.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!