Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda