Skip to content

Instantly share code, notes, and snippets.

@travissanon
Last active November 12, 2017 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travissanon/3befe5ce3f0ae2e529bda6d0d9044099 to your computer and use it in GitHub Desktop.
Save travissanon/3befe5ce3f0ae2e529bda6d0d9044099 to your computer and use it in GitHub Desktop.
var vs let vs const
var, let, and const. What are the differences of the three and which ones should you use?
If you do not know what let and const are, they are basically different variations of the “variable”, derived from ES6.
Variables are absolutely essential to any programming language, but in Javascript, the var keyword does has some flaws.
Thats where ES6 variables come in.
When you declare a const = Cannot be updated
When you declare a let = The variable can only be used in the block its created in
What do I mean by “block”?
Anything inside a pair of curly braces is a block.
Now lets take a look at some examples to better understand var let and const
// const
const yo = ‘Hello’;
yo = ‘Hey’;
// console: **ERROR**
// let
function greetings(){
if (true) {
let greeting = ‘whats up’;
}
console.log(greeting);
}
// console: **ERROR**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment