ociqzkzg
4 months ago  Karma: 416
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
4 months ago  Karma: 416
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
4 months ago Karma: 1469

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