in JavaScript by
Q:
Guess the output of the following code:

var x = 23;

(function(){

  var x = 43;

  (function random(){

    x++;

    console.log(x);

    var x = 21;

  })();

})();

1 Answer

0 votes
by
Output is NaN .

random() function has functional scope, since x is declared and hoisted in the functional scope.

Rewriting the random function will give a better idea about the output:

function random(){

  var x; // x is hoisted

  x++; // x is not a number since it is not initialized yet

  console.log(x); // Outputs NaN

  x = 21; // Initialization of x

}
Click here to read more about JavaScript
Click here to read more about Insurance

Related questions

0 votes
asked Feb 6, 2020 in JavaScript by rajeshsharma
0 votes
asked Jan 18 in Python by SakshiSharma
...