Skip to content

Instantly share code, notes, and snippets.

@ristaa
Created September 12, 2018 08:35
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 ristaa/4fc69fbd3425ed515ff4152a1d28585e to your computer and use it in GitHub Desktop.
Save ristaa/4fc69fbd3425ed515ff4152a1d28585e to your computer and use it in GitHub Desktop.
Update and re-declare of let and const
// This is OK - updating apples
let apples = "3 apples";
apples = "4 apples";
// This is NOT OK - re-declare pears
let pears = "3 pears";
let pears = "4 pears"; // error: Identifier 'pears' has already been declared
// This is OK - different scopes, same variable name
let bananas = "3 bananas";
if( true ){
let bananas = "4 bananas";
console.log(bananas); // "4 bananas"
}
console.log(bananas); // "3 bananas"
// CONST part
// This is NOT OK - updating const
const appleConst = "3 apples";
applesConst = "4 apples"; // error: Assignment to constant variable.
// This is NOT OK - re-declare const
const pearsConst = "3 pears";
const pearsConst = "4 pears"; // error: Identifier 'pearsConst' has already been declared
// This is OK - update property of CONST object
const showMe = {
what: "apples",
howMany: 3
};
showMe.howMany = 4;
console.log(showMe.howMany + ' ' + showMe.what); // "4 apples" - property howMany was updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment