75

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;
4
  • 6
    I'll go with option b Commented May 10, 2013 at 3:39
  • 3
    @Mr.Alien - agreed. There is no reason to use the = null version unless it actually matters that the variable values are null instead of undefined. Commented May 10, 2013 at 3:47
  • Actually, var a, b; mightn't always leave it undefined. In former IE versions, you would need to change that piece of code to var a = undefined, b = undefined;, otherwise your code would generate an error while trying to access it (even if you were just trying to simply check if it has some value with if (a && b), for example). In that case, an unset variable differs from undefined. Commented Jul 8, 2015 at 21:32
  • Another question, is there a difference in memory management stackoverflow.com/questions/68339963/… Commented Jul 11, 2021 at 21:10

9 Answers 9

62

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.

Sign up to request clarification or add additional context in comments.

Comments

28

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

2 Comments

Then is it necessary to 'assign' undefined, because as you said they are undefined after all?
As mentioned it's not necessary to declare undefined, but also if you do declare undefined then when testing that value you won't know if you deliberately set it or if it was never actually set. This is why I prefer null in cases where there may not be a value.
16

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.

Comments

10

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

Comments

5

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.

Comments

3

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

2 Comments

Checking for null in this case is not good. As it violates the "avoid null comparsions" best practice in javascript. You should compare against null only for DOM object (my.safaribooksonline.com/book/programming/javascript/…)
I don't agree; all we want to do is see if a variable is actually null or undefined - which are totally valid checks. I do agree that in your cited example it's a bad idea to check against null, but that's just one case :)
3

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

Comments

1

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

Comments

-19

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

1 Comment

An empty string and null are very different things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.