Introduction to ES6
ES6, stands for ECMAscript version 6, is the standard for new additions to Javascript
every year, a new version of ES is created, adding more abilities to JavaScript base library
var hoisting
a declaration of a variable with var is handled even if should not , by the flow
if(false) {
var phrase = "Hello";
}
console.log(phrase);
the variable is declared but not assigned
This is one of the reason var was deprecated in ES6!!!
Change 1:
Instead of var, ES6 started working with....
let & const
let & const are valid inside the block they are defined in and in it's decendents
- let is a variable that may change during the flow
- const is a read-only variable whose value will never change,
- Usually declared in CAPITAL_LETTERS
- Must be declared and assigned together
declaration of let and const must be before the reference to them
const STR; //error
STR = 'hello' //error
const myStr = 'hello!'; //working but not best practice
const MY_STR = 'hello!'; //The right way
When a const is an object, the attributes of the object can be changed, although its a const.
const PERSON = {
Name: 'David';
};
PERSON.Name = 'Moshe'; //allowed
To block all attributes of a const object from changing, declare the const with Object.freeze
const PERSON= Object.freeze({
Name: 'David',
})
PERSON.Name = 'Moshe'; //will not be assigned
console.log('PERSON: ' , PERSON.Name) //will return David
back to main page