JavaScript

:Wanted help for Difference between Var and Let.I have issue that declaring variable using let inside loop cannot be acessed outside loop. What is main difference between let and var.

i can not help with var and let but the course say it recommended to use let

but for why you can not access variable declared inside loop cause for 2 reason

  1. you tried to access it before declare it
    or
  2. you can not access variable outside it’s scope for example
    function hi (){
    let x;
    }
    function hello(){
    let y;
    here you can not access x cause it in different block scope
    }

that way you safe variable from access out of it scope or you will face that you can access ad change any variable from any code which can make thing unprotected

check this https://developer.mozilla.org/en-US/docs/Glossary/Scope

Thanx for reply.I have used let after declaring only.But i have used this:

let myNewArr = [“Apple”, “Mango”, “Oranges”];

for(let i = 0; i < 5; i++)
{
let myArr = myNewArr[i];
}
console.log(myArr);

In this we cannot log myArr variable outside loop which is declared inside loop.

you welcome yes

myNewArr is accessible everywhere

myArr only inside the loop

you can think of it as if it inside bracket then it only accessible inside those bracket

have a nice day