Can I use a JavaScript variable before it is declared?

JavascriptWeb DevelopmentFront End Technology

Yes, you can use a JavaScript variable before it is declared, with a technique called hoisting. The parser read through the complete function before running it.

The behavior in which a variable appears to be used before it's declared is known as hoisting −

For example, the following,

rank = 5;
var rank;

The above works the same like the following −

var rank;
rank = 2;


raja
Published on 16-Jan-2018 11:12:02
Advertisements