C

#include <stdio.h> int main () { int n = 5; if (1) { int n = 2; } printf("n is %d", n); }

n is 5

var scoping = function () { var n = 5; if (1) { var n = 2; } alert('n is ' + n); };

If you answered n is 5 , then you are wrong



C

var scoping = function () { var n = 5; var x = function () { var n = 2; }; alert('n is ' + n); };

n is 5

n





Those developers who are coming from a C background may find JavaScript's scoping rules a bit confusing.Let's take a look at the followingcode:The output of the above code is:Now let us implement the above code in JavaScript:What do you think the above code alerts?This is because, unlikeand not Block Scope.What this means is that JavaScript creates a new scope only with a function:Now,is alerted because a new scope has been created with the inner function and thus it's encapsulating the inner variable