Sun Aug 16 2015
Paul Shan
function func() { if (true) { var myVar = 100; let myLet = 200; } console.log(myVar); // 100 console.log(myLet); // ReferenceError: myLet is not defined }
function func() { if (true) { console.log(myVar); // 100 console.log(myLet); // ReferenceError: myLet is not defined var myVar = 100; let myLet = 200; } }In case of let, the variable is completely unaccessible till the time the control reaches the line of declaration.
function func() { if (true) { var myVar = 100; let myLet = 200; var myVar = 100; // OK let myLet = 200; // Error: already declared } }If the function has a parameter named "myParam", you cant declare myParam again inside the function with let.
function func() { let arr = []; for (let i=0; i < 3; i++) { arr.push(i); } console.log(arr); // [0,1,2] console.log(i); //Error: not defined. If i was a var, the value would have been 3 here }
var myVar = 100; console.log(window.myVar); // 100 let myLet = 200; console.log(window.myLet); //Undefined
function func() { let arr = []; for (const i=0; i < 3; i++) { //Error: "i" is read-only arr.push(i); } console.log(arr); // [0,1,2] }
function func() { const myConst = 100; myConst=200; //Error: read only }
function func() { const myConst; //Error }
function func() { const me = {}; me.name = "Paul"; //OK const people=[]; people.push({}); //OK me = {}; //Error: read only people = [1,2,3]; //Error: read only }
function func() { const me = Object.freeze({}); me.name = "Paul"; //Error const people=Object.freeze([]); people.push({}); //Error }
var | let | |
---|---|---|
Scope | Function scoped | Block scoped |
Multiple declaration in the same scope | No issues | Throws error |
Temporal dead zone | No | Yes |
Access outside loop | Yes | No |
Declaration conflict with function params | No | Yes |
Global scope | Exists in global scope as well as become a property of the global object. | Exists in global scope but doesn't become a property of the global object. |
function func () { let obj = {first:"Paul", last:"Shan"}; let { first: f, last: l } = obj; console.log(f + ' ' + l); // Paul Shan }
function func () { let arr = ["Paul", "Shan"] let [x, y] = arr; console.log(x + ' ' + y); // Paul Shan }
// {x,y} is the same as { x: x, y: y } function func (argument) { let obj = {first:"Paul", last:"Shan"}; let { first, last } = obj; console.log(first + ' ' + last); // Paul Shan }
let obj = { myProp: someValue, // property myMethod(arg1, arg2) { // method definition } };If you notice, this is not a valid JSON object; but this is the new object declaration style.
let propKey = 'firstName'; let obj = { [propKey]: "Paul", ['last'+'Name']: "Shan", }; console.log(obj.firstName + " " + obj.lastName); // Paul Shan
SHARE THIS ARTICLE
Thu Mar 10 2016
OAuth authentications are pretty popular now a days and another thing which is popular is JavaScript. This article shows how to plugin google’s oAuth api for authentication in your own node application.Sat Mar 01 2014
This is a continuation of my CSS3 loader snippet collection series. I've provided spinning css3 animation loader in the part 1 of this series and here in part 2, I'm providing various square type loading