0

I'm new to the JavaScript World and I'm confused after I knew a lot about the global object (usually window) and knew it's just an object like any object I'd create and that var set properties in the global object unlike let

after that Is there any difference between window.a = something (like in any object) and var a = something?

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
Ahmed Hamed
  • 181
  • 1
  • 8

3 Answers3

2

In the global context, using var vs assigning to window are indeed quite similar. However, yes there are a number of differences. Here are a few I can think of:


  • var declarations are hoisted, which means that you can use a variable declared with var before you've declared it. On the other hand, trying to use something assigned to window before that assignment has occurred will produce a ReferenceError:

// This is okay. The variable declaration is hoisted, so you can use it before
// it's been declared (although it won't be assigned its value until later).
console.log(a);
var a = "hello world";

// On the other hand, without var, this creates an error.
console.log(a);
window.a = "hello world";

  • Variables declared with var cannot be removed from the global object, but simple assignments to window can be removed:

var a = "hello world";

console.log(a);
delete window.a; // Does nothing for `var`.
console.log(a);

window.a = "hello world";

console.log(a);
delete window.a; // This will delete it, further access creates a ReferenceError.
console.log(a);

  • And, of course, var declarations are scoped to the current execution context. In the global scope, this doesn't differ from assigning to window, but within a function, a var will disappear when the function returns.

function foo() {
  var a = "hello world";
  console.log(a);
}

foo();
console.log(a); // ReferenceError

function foo() {
  window.a = "hello world";
  console.log(a);
}

foo();
console.log(a); // still exists here

CRice
  • 29,968
  • 4
  • 57
  • 70
  • OK, but like you say in the third difference var declarations are scoped to the current execution context. but window is not. **Why when assigning a value to something without declaring it with var creates a global object (inside a container of course)? is this equal to window.something** @crice – Ahmed Hamed Oct 04 '18 at 22:46
  • Yes, assigning a value to an *undeclared* variable is the same as assigning to `window` for the most part. However, *when in strict mode*, the former will be an error but the latter is still allowed. If you're not in strict mode, then the two are the same afaik. – CRice Oct 04 '18 at 22:55
0

You use window.a = if you want the variable a global. This means that any JS code has access to this variable. While the var a = is the commonly used way to declare a variable. In this case, the variable is only accessible inside its container.

Erlisar Vasquez
  • 460
  • 3
  • 13
0

Nope, except for in Node.js, where having a = 5 or var a = 5 (along with let and const) will not assign the value to global.a. You have to explicitly say global.a = 5.