ES6 Variables

ES6 Variables

 

 


Now, with ES6, there are three keywords available to define variables: They provide different levels of scope to access the variable. Based on the requirement, you can define variables with these keywords and control its accessibility.

  • var 
  • let
  • const

 

Using ES6 var

Example

var x = 9.9;

If var declared outside a function, it belongs to the global scope.

If
var declared inside a function, it belongs to that function.

If
var declared inside the block, ex: in a for loop, the variable is still available outside that block.

var has a function scope, not a block scope. 

Using ES6  let

Example

 

let x = 9.9;



let is the block scoped version of var, and is limited to the block, where it is defined.

If you use let inside a block, i.e. a for loop, the variable is only available inside that loop.

let has a block scope.


Using ES6 const

Example

const  y = 9.9;

 y=10;   // It will result in an error  
 

constant is a read only variable. Once it is initialized, it cannot be changed.

const has a block scope.
 
Actually It defines a constant reference to a value, not
not define a constant value.

With this reason, you can NOT Reassign:

    a constant value
    a constant array
    a constant object

    But you CAN change:
    the elements of constant array and
    the properties of constant object
 

Comments

Popular posts from this blog

Recently viewed list in PHP Mysql

Import CSV File Into MySQL Using PHP

Shoppingcart using PHP Sessions - Miniproject