33
loading...
This website collects cookies to deliver better user experience
var name = ‘Tripp’
function sayName(){
return name
}
console.log(name)
//returns ‘Tripp’ and also prints ‘Tripp’
function spellName(){
let name=‘tripp’
for(letter of name){
console.log(letter)
}
//Will print out each letter of name variable
console.log(name)
//returns reference error. This is because we are trying to access the variable outside of the function that it was declared in.
{}
. This applies to the keyword let
and const
. IMPORTANT: var
does not have block scope.//example of creating a block
let dog = ‘Ada’
{
let name=‘Tripp’
let dog = ‘Taz’
console.log(dog)
//prints “Taz”. Has access to the dog variable inside of the block. Chooses to print “Taz instead of “Ada” because of scope chain… More on that shortly.
}
console.log(dog)
// prints ‘Ada’. Block scope prevents access to the dog variable ‘Taz’ inside of the block. It does have access to the global scope of dog ‘Ada’ and prints it.
{ }
you are generally starting a new scope.