Set JavaScript variable = null, or leave undefined?

Javascript

Javascript Problem Overview


When declaring variables at the top of the JavaScript function, is it best practice to set them equal to null, or leave as 'undefined'? Another way to ask, what circumstances call for each option below?

Option A:

var a = null,
    b = null;

Option B:

var a,
    b;

Javascript Solutions


Solution 1 - Javascript

It depends on the context.

  • "undefined" means this value does not exist. typeof returns "undefined"

  • "null" means this value exists with an empty value. When you use typeof to test for "null", you will see that it's an object. Other case when you serialize "null" value to backend server like asp.net mvc, the server will receive "null", but when you serialize "undefined", the server is unlikely to receive a value.

Solution 2 - Javascript

I declare them as undefined when I don't assign a value because they are undefined after all.

Solution 3 - Javascript

Generally, I use null for values that I know can have a "null" state; for example

if(jane.isManager == false){
  jane.employees = null
}

Otherwise, if its a variable or function that's not defined yet (and thus, is not "usable" at the moment) but is supposed to be setup later, I usually leave it undefined.

Solution 4 - Javascript

Generally speak I defined null as it indicates a human set the value and undefined to indicate no setting has taken place.

Solution 5 - Javascript

I usually set it to whatever I expect to be returned from the function.

If a string, than i will set it to an empty string ='', same for object ={} and array=[], integers = 0.

using this method saves me the need to check for null / undefined. my function will know how to handle string/array/object regardless of the result.

Solution 6 - Javascript

Be careful if you use this value to assign some object's property and call JSON.stringify later* - nulls will remain, but undefined properties will be omited, as in example below:

var a, b = null;

c = {a, b};

console.log(c);
console.log(JSON.stringify(c)) // a omited

*or some utility function/library that works in similar way or uses JSON.stringify underneath

Solution 7 - Javascript

The only time you need to set it (or not) is if you need to explicitly check that a variable a is set exactly to null or undefined.

if(a === null) {
    
}

...is not the same as:

if(a === undefined) {

}

That said, a == null && a == undefined will return true.

Fiddle

Solution 8 - Javascript

There are two features of null we should understand:

  1. null is an empty or non-existent value.
  2. null must be assigned.

You can assign null to a variable to denote that currently that variable does not have any value but it will have later on. A null means absence of a value.

example:-

 let a = null;
 console.log(a); //null

Solution 9 - Javascript

You can use ''; to declaring NULL variable in Javascript

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrgwozdzView Question on Stackoverflow
Solution 1 - JavascriptKhanh TOView Answer on Stackoverflow
Solution 2 - JavascriptSteven WexlerView Answer on Stackoverflow
Solution 3 - JavascriptKennyCView Answer on Stackoverflow
Solution 4 - JavascriptVishal SakariaView Answer on Stackoverflow
Solution 5 - JavascriptsniradView Answer on Stackoverflow
Solution 6 - JavascriptbarbsanView Answer on Stackoverflow
Solution 7 - JavascriptjterryView Answer on Stackoverflow
Solution 8 - JavascriptSaloni MalhotraView Answer on Stackoverflow
Solution 9 - JavascriptJackView Answer on Stackoverflow