Skip to content

Instantly share code, notes, and snippets.

@koficypher
Created February 18, 2019 19:23
Show Gist options
  • Save koficypher/d6bccebb8396571438e8cdc6b6b07b26 to your computer and use it in GitHub Desktop.
Save koficypher/d6bccebb8396571438e8cdc6b6b07b26 to your computer and use it in GitHub Desktop.
/***
Name: Kofi Cypher
Email: [email protected]
Var
The var keyword is used to declare variables globally or locally within a fucntion block
It is suitable for use when you want to declare variables you would want to throughout your code execution.
Let
The let keyword is also used to declare variables. Here variables declared will only be available in the block where they were declared.
This could be an if block or a function block. Suitable for declaring variables that are meant to be short lived or needed only in an
execution block.
Const
The const keyword is used in declaring variables whose values will not change in the entire code execution. The cannot be declared
without values. They are also block scoped like variable declared with the let keyword.
Hoisting
Hoisting in simple terms is when the execution/invoking of a function is done before the actual function or the assignment of a
variable is done before the variable is declared. Usually found at the top of the function declaration or variable declaration.
***/
//Var
var fruit = 'mango';
if (fruit === 'mango') {
var fruit = 'orange';
console.log(fruit); //outputs orange
}
console.log(fruit); //outputs orange
//Let
let animal = 'bear';
if (animal === 'bear') {
let animal = 'rabbit';
console.log(animal); //outputs rabbit
}
console.log(animal); //outputs bear
//Const
const instrument = 'guitar';
try {
instrument = 'voilin';
} catch(err) {
console.log(err); //returns an error
}
console.log(instrument); //outputs guitar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment