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

174
Views
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 answers
Answer question

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 Report

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 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!