Computer >> Computer tutorials >  >> Programming >> Javascript

Can I use a JavaScript variable before it is declared?


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;