Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

If I have a declaration as follows:

var j; 

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

like image 677
thisissami Avatar asked May 12 '12 01:05

thisissami


People also ask

What is the default value of variable in JavaScript?

In JavaScript, function parameters default to undefined . However, it's often useful to set a different default value.

What happens when you declare a variable in JavaScript?

Declaring a JavaScript Variable or: let carName; After the declaration, the variable has no value (technically it is undefined ).

When a variable is null in JavaScript?

In JavaScript, null is a special value that represents an empty or unknown value. For example, let number = null; The code above suggests that the number variable is empty at the moment and may have a value later.

How is a variable declared in JavaScript?

Basically we can declare variables in three different ways by using var, let and const keyword. Each keyword is used in some specific conditions. var: This keyword is used to declare variable globally. If you used this keyword to declare variable then the variable can accessible globally and changeable also.


1 Answers

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.

like image 131
Danilo Valente Avatar answered Sep 28 '22 19:09

Danilo Valente