0

I am learning javascript and I am unable to understand difference between global variable and window variable and in which namespace the variables will be stored

For example,

var a = 1;
alert(window.a);

This will create alert window with message '1'.

let b = 1;
alert(window.b);

This will create alert window with message 'undefined'.

But if b is not getting created in window space, where it is getting created. When we run a javascript program, will 2 namespaces be created (one for global and one for window)?

kadina
  • 5,042
  • 4
  • 42
  • 83

1 Answers1

-1

When the JavaScript code runs in a browser, the global variables created by the user code using var are properties of the global window object.

let declares a variable that exists only in the block where it is declared.

At the top level of programs and functions, let, unlike var, does not create a property on the global object.

A block statement (also known as "compound statement") is used to group zero or more statements. The block is delimited by { and } and it can be used anywhere a single statement can be used.

axiac
  • 68,258
  • 9
  • 99
  • 134