Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda