var a = 2;
if (true) {
var a = 5;
console.log(a) // 5
}
console.log(a) // 5
I'm new to Javascript, can anyone tell why it printed 5 in below console statement
var a = 2;
if (true) {
var a = 5;
console.log(a) // 5
}
console.log(a) // 5
I'm new to Javascript, can anyone tell why it printed 5 in below console statement
For further actions, you may consider blocking this person and/or reporting abuse
Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.
Arindam Majumder -
Tib -
Paul Osadchuk -
Okoye Ndidiamaka -
Top comments (2)
TL;DR If you use
var
this will happen, try usinglet
var
is not block scoped, this is a limitation from the days of old JavaScript. Nowadays you should use the modernlet
orconst
.Some resources:
You overwrote the variable in the if statement.