React ES6 Variables


Variables

Before ES6 there was only one way to describe your variable: by the keyword var. If you do not describe them, they will be assigned to an object of the world. Unless you were in solid mode, you would get an error if your variables were not defined.

Now, with ES6, there are three ways to define your variable: var, let, and const.


var

var x = 5.6;
        

If you use var without work, it is a global scope.

If you use var within a function, it belongs to that function.

If you use a var inside a block, i.e. a loop, the exception is still found outside that block.


var has a broad scope of activity, not a block scope.


let

let x = 5.6;
        

let the block scoped version of var, and be limited to the block (or expression) in which it is defined.

If you use let inside of a block, i.e. loop, a variable that is only found within that loop.


let has a block scope.


const

const x = 5.6;
        

const is a variable that once created, its value will never change.


const has a block scope.


The key word const is slightly misleading.

It does not specify a fixed value. Defines a fixed reference value.

Because of this you can NOT:

  • Reassign a constant value
  • Reassign a constant array
  • Reassign a constant object

But you CAN:

  • Change the elements of constant array
  • Change the properties of constant object