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

295
Views
What is the behavior of the variables here, i.e. closure. The output is undefined which I don't understand

What is the behavior of the variables here, i.e. closure. The output is "undefined" which I don't understand.

var x = 21; 
var test = function () { 
    console.log(x); // output: undefined 
    var x = 20; 
}; 
test();
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Javascript creates a global execution context initially and a new execution context every time a function is pushed on the call stack. This happens in two phases: it first creates a variable environment where every variable is stored in a key, value pair of variable_name: undefined (whereas the functions are stored with the function definitions). It then starts executing the code line by line.

If I were to walk through this example: Global execution context: Variable Environment { x: undefined, test: function definition for test }

Then the code is executed line by line and 21 replaces undefined for the value of x. Now test is called, so a new execution context is created

Test's execution context: { x: undefined }

Now, when it starts executing the code, it looks for x within this execution context and sees the value is undefined, and logs it. If x wasn't present in test, it would have looked at the function's parent's execution context, which happens to be the global execution context, and would have logged 21.

Read up on execution context: https://www.javascripttutorial.net/javascript-execution-context/ Then read up on lexical scope to understand this behavior.

about 4 years ago · Juan Pablo Isaza Report

0

Javascript engine will explain your code like this:

var x = 21; 
var test = function () {
    var x;
    console.log(x); // output: undefined 
    x = 20; 
}; 
test();
about 4 years ago · Juan Pablo Isaza Report

0

You forget to put parameter in function test the code should be like this:-

var x = 21; 
var test = function (x) { //here u should put parameter "x"
    console.log(x); // The output from calling method
    var x = 20; //last "x"
    //if u want to get the value of last "x" should call 
    console.log(x); // the value of of last "x"
}; 
test(x); //here u should put parameter "x"

in code i explain every thing 😉

Notes:- Variables defined with let cannot be Redeclared. Variables defined with let must be Declared before use. Variables defined with let have Block Scope. Redeclaring a variable with let, in another block, IS allowed but with var Not-allowed

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!