Skip to content

Instantly share code, notes, and snippets.

@siddrc
Last active May 13, 2018 07:06
Show Gist options
  • Save siddrc/3f9d345dcdcb82b1eb86a16da1a8aecb to your computer and use it in GitHub Desktop.
Save siddrc/3f9d345dcdcb82b1eb86a16da1a8aecb to your computer and use it in GitHub Desktop.
ES6 using var-let-const

Introduction

es6

In learning ES6 the first we learn is to declaring variables. var-let-const-2

ES6 dictates the use of let and const in declaring variables. Out of all the developer videos and articles 3 simple rules emerge in using var-let-const

  1. var is totally discouraged in ES6 specification.
  2. Everything and all variables are to be declared using const by default.
  3. Use let if value of the variable changes.

9-es6-let-and-const-typescript-javascript-3-638

try the below snippets in your editor

if(true){
  var someVariableName = "hello world"
}
console.log(someVariableName) //leaks outside the if block, var is not block scoped - :(

Now, try the below code.

if(true){
  let someVariableName = "hello world"
}
console.log(someVariableName) //does'nt leak outside.Infact the code will give an error.

const is for mostly declaring variables which never change their value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment