ociqzkzg
10 days ago  Karma: 151
JavaScript: is it possible to declare variables which are bound to a block?

Is it possible to declare variables which are bound to a block?

en
#javascript
ociqzkzg
10 days ago  Karma: 151
JavaScript: is it possible to declare variables which are bound to a block?

Is it possible to declare variables which are bound to a block?

en
#javascript

ONE ANSWER
brandon.pearson1011
10 days ago Karma: 867

Yes, JS supports block scoping. You just need a pair of { }.
Also, instead of using var, which declares variables attached to the enclosing function, use let.

var x=1;
{
let x=2;
console.log(x);
}
console.log(x);

Yes, JS supports block scoping. You just need a pair of { }.
Also, instead of using var, which declares variables attached to the enclosing function, use let.

var x=1;
{
let x=2;
console.log(x);
}
console.log(x);

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER