Skip to content

Instantly share code, notes, and snippets.

@LNA
Last active May 5, 2016 15:11
Show Gist options
  • Save LNA/ab481dc18bc4752a93dff44dabf6a0ee to your computer and use it in GitHub Desktop.
Save LNA/ab481dc18bc4752a93dff44dabf6a0ee to your computer and use it in GitHub Desktop.
//Using let
//the let keyword scopes to the nearest block, not function
// it prevents variable declarations from being moved to the top of the scope. This is called hoisting.
// loadingMessage and flashMessage are trapped in their braces, and can't be accessed outside of them.
<script src="./load-profiles.js">
<script>
loadProfiles(["Prince", "MJ", "James Brown"]);
</script>
function loadProfiles(names){
if(names.length > 3){
let loadningMessage = "This might take a while";
} else {
let flashMessage = "loading";
}
}
// Using let in for loops
//using let instead of var will return the proper username, instead of the num it it in the iteration
function loadProfiles(names){
for(let i in useNames){
_fetchProfile("/users" + userNames[i], function() {
console.log("Fetched for ", userNames[i]);
});
}
}
// Reassigning with Let
let name = "LaToya";
name = "Jayden";
console.log( name ); // returns Jayden
//But be careful!
let name = "LaToya";
let name = "Jayden";
console.log( name ); //generates a type error
//Using const to clarify code
//Like let, const ivariables declared with const are scoped to the nearest block.
const MAX_USERS = 3;
const MAX_REPLIES = 3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment