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

165
Vistas
Why the code execution result is 10 ? JS syntax

The code is:

x = 10;

if (x > 1) {
  var x = x + 1;
}

console.log(x);
var x;

The output of code execution is: 11

Why is it 11? , And Why is it not an error?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Description

var declarations, wherever they occur, are processed before any code is executed. This is called hoisting and is discussed further below.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#description

This means it does not matter where is your "var x", when this script is processed, the declaration will be the first.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should understand how closures actually work.

// You define 'x' variable [1] in global scope 
x = 10;


if (x > 1) {
  // You define another one 'x' variable inside of 'if' scope
  // So here (inside of 'if') you will interact with this variable,
  // not with first one ([1])
  var x = x + 1;
}

// You code run out of 'if' so you're working with 
// global scope again, and your 'x' is first one [1]
console.log(x); // x = 10 still 
var x;
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