Created
September 12, 2018 08:35
-
-
Save ristaa/4fc69fbd3425ed515ff4152a1d28585e to your computer and use it in GitHub Desktop.
Update and re-declare of let and const
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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