Global and local scope variable

Asked

Viewed 189 times

7

If I print the global scope variable within a local scope, I am not allowed to redeclare again in the local scope. Why? For example, if I do:

  let a = 2;
    {
      let a = 3;
      console.log(a) //aqui aparece 3
    }
       console.log(a) //aqui aparece 2

So far so good. But if I do:

 let a = 2;
    {
      console.log(a)//aqui aparece 2
      let a = 3; //aqui ja da erro, não aceita

    }

3 answers

9


You can declare again, that’s not the problem of the code you posted.

let a = 2;
{
    console.log(a); //aqui aparece 2
    let a = 3; // aqui ja da erro, não aceita
}

You wrote aqui aparece 2, but that’s not what’s happening. The problem is in itself console.log(a), not in the let a = 3.

What’s going on here is called hoisting. Javascript reserves all variables declared in the local scope when starting the scope, but despite this let a already be booked, you cannot access it until after the line it has been declared.

That is, the console.log(a) is trying to access the let a local, but the let a location is not yet valid, and this is the source of the error.

Also note that this is the behavior only of variables declared as let or const. Variables declared as var can be accessed before your declaration without releasing exceptions, however its value will be undefined.

  • Great answer, well noted that there does not appear 2.

9

I wouldn’t advise doing this, but with var you can do it. The let actually working with the statement in the right order, even was the reason it was created. The wrong was the var and if you want to do something like this, you should justify the reason. No designer language needs to justify something that language does not do. They needed to justify why the var I was able to get two variables with the same name in the same scope, which makes no sense and creates confusion. Because they couldn’t justify it and it was considered a mistake they created a way that didn’t do it.

With the use of var the first console.log() is accessing the variable a more global scope and the second is accessed another more local scope variable, but people will find that it is the same variable because it has the same name. I think it would be interesting to understand what a variable.

The var does something called Hoisting, the let does not, never so in its code it is not this that happens, so much that it gives error if you try the same thing that would cause the Hoisting. Behold What is the difference between variable declaration using Let and var? and Best practices in Javascript variable declaration.

var a = 2;
{
    console.log(a)
    var a = 3;
    console.log(a)
}

I put in the Github for future reference.

3

Your code is not working because you have not changed its scope. Wrapping the code with {} does not mean that it is in a different scope. To change scope you must create a function. Follow the example:

let a = 1;

function test() {
  let a = 2;
  console.log(a);
}

console.log(a);

test();

It is not because the above code works that it is the best option. It is not recommended to use two identical names in your code. If both names are equal, it means that both have the same value and data type, so they do not need to be declared twice, only receive a new assignment when necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.