1

In javascript, is there any functional difference between using != null and != undefined?

Is there a value you can assign to myVar which will cause these two lines of code to evaluate to different results?

console.log(myVar != undefined)
console.log(myVar != null)

And if you know anything about the performance of these two operations, I'd love to know about that too.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
joehinkle11
  • 491
  • 1
  • 4
  • 9
  • 1
    Performace should be the least of your concerns. What are the possible values of `myVar`? – Joseph Feb 13 '19 at 16:06
  • 1
    Possible duplicate of [What is the difference between null and undefined in JavaScript?](https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript) – r3zaxd1 Feb 13 '19 at 16:12
  • @r3zaxd1 Useful link, thanks. But my question is different. It's about the inequality operator, not the nature of null and undefined compared. – joehinkle11 Feb 13 '19 at 16:57
  • 1
    @Teemu I mistyped, I'll edit and fix that now. – joehinkle11 Feb 13 '19 at 16:58

4 Answers4

2

There is no functional difference. x != undefined and x != null both only evaluate to false when x is null or undefined. They both evaluate to true for all other values of x.

There is also no performance difference.

Paul
  • 139,544
  • 27
  • 275
  • 264
1

There is no difference as you can see in below table for JS == testing (focus on null/undefined row/column) (src: here). So myVar!=null is true only if myVar value is not null and not undefined (same with myVar != undefined)

enter image description here

It looks like both has similar performance (I made test on Mac OS X 10.13.4 HighSierra: Chrome 71.0.3578, Firefox 65.0.0 and Safari 11.1.0 - you can run test in your browser here)

let myVar1=null;
let myVar2=undefined;

enter image description here

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
0

The == and != operators make "type conversion" to compare just the value itself. Then no, there's no difference using "undefinied" or "null" is this case, both represents "empty".

But if you use === and !== instead, it checks the type and the value, without any type casting. The results for the two lines would be different.

myVar = null;
console.log(myVar !== undefined) //true
console.log(myVar !== null) //false
Pedro leal
  • 174
  • 2
  • 8
-2

Don't confuse undefined and null as they are not the same thing.

null:

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.

undefined:

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.


If the variable consists of a value that is neither null nor undefined, then no there is no difference in your condition.

const value = 3;

console.log(value !== undefined) //true
console.log(value !== null) //true

However, a better way of testing if the variable is null or undefined is to use ! negation as the value null or undefined will be resolved to true.

const undefinedValue = undefined;
const nullValue = null;

console.log(!undefinedValue);
console.log(!nullValue);

Here are some examples of it.

var someVariable = undefined;

console.log(someVariable !== undefined, "; undefined !== undefined");
console.log(someVariable !== null, "; undefined !== null");

var someVariable = null;

console.log(someVariable !== undefined, "; null !== undefined");
console.log(someVariable !== null, "; null !== null");


var someVariable = undefined;
console.log(!someVariable);

var someVariable = null;
console.log(!someVariable);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131