1

The terms "initialization" and "assignment" seem to be used interchangeably. I did some searching and it seems that there might technically be a difference. My understanding is that, in the context of variables, a variable is initialized when the JavaScript engine makes it available for use, and assignment (whether done explicitly [as in let foo = 1;] or by the JavaScript engine, as in the following example) is one way to achieve this.

let foo;
console.log(foo); // undefined (initialization and assignment?)

Is my understanding correct? Also (if so), what actually occurs during initialization to make the variable available?

Deja
  • 3,278
  • 2
  • 18
  • 46
  • 1
    you mix up declaring and initializing. the first assignment is the initializing. the declaring makes the variable available. – Nina Scholz Jun 17 '18 at 09:01
  • initialization and assignment are not the same. Initialization creates the contents of the memory that a variable refers to. Assignment just makes a variable refer to some memory. – Tibrogargan Jun 17 '18 at 09:02
  • The binding is the hoisting for let and const--is this the same thing as declaring? (I thought the keyword and variable name comprise a declaration) – Deja Jun 17 '18 at 16:20

1 Answers1

4

TLDR:

{ // declaration (hoisted)
  // Temporal deadzone
  let foo; // declaration and initialization to undefined
  foo = 1; // assignment
}

A bit longer:

Declaration

Declaring a variable means that we reserve the identifier at the current scope. In javascript declarations are hoisted, that means that it gets declared when the scope the variable is in gets visible (the block it is in gets executed). However you cannot access that variable now as it is in

The temporal deadzone

This is a specific part of the code that is between the beginning of the scope and the initialization. Trying to access the variable here results in an error.

Initialization

The initialization takes place in the line were you declared the variable. It will assign a value to the variable and will make it available for access. This for example:

let foo;

will initialize foo to undefined,

let foo = 2;

will initialize foo to 2.

Assignment

...just means that you change the value of a variable. All assignments in javascript use =. The initialization is basically just the first assinment.

The explanation above does not apply to variables declared with var, so just don't use var to avoid confusion :)

Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
  • @stefan yes partly, what i described above does not apply to `var`, but `let` and `const` are hoisted. https://stackoverflow.com/questions/33198849/what-is-the-temporal-dead-zone – Jonas Wilms Jun 17 '18 at 09:59
  • I'm sorry. I read the MDN page again. It looks like I had a misconception about let statements that made me think they were not hoisted. – Stefan Octavian Jun 17 '18 at 10:03
  • @stefan no worries, different people use different words in different languages to describe basically the same thing, I get confused too quite often... – Jonas Wilms Jun 17 '18 at 10:07
  • The hoisting of const and let is only the creation of the binding (storage space) at the top of the relevant scope, right? But you are saying the declaration is hoisted... Is the declaration keyword (as in "let")? Or it just the binding (no relation to the keyword)? – Deja Jun 17 '18 at 16:16
  • @natalie exactly just the binding, the declaration keyword (let or const) then marks the initialization – Jonas Wilms Jun 17 '18 at 16:55