thisissami
thisissami

Reputation: 16373

When declaring a variable in javascript, is the default value null?

If I have a declaration as follows:

var j;

does j==null until I set it equal to something?

Upvotes: 26

Views: 32768

Answers (1)

Danilo Valente
Danilo Valente

Reputation: 11352

No, it has the default value of undefined
But if want to use the !j condition, it will work with both the values (i.e. undefined or null)

Note that (j==null) is true, but (j===null) is false... JavaScript have "falsy" values and sometimes unexpected rules to convert values, plus fancy === operator to compare value and type at the same time.

Upvotes: 43

Related Questions