Why Is Null an Object and What's the Difference Between Null and Undefined

What is the difference between null and undefined in JavaScript?

undefined means a variable has been declared but has not yet been assigned a value :

var testVar;
alert(testVar); //shows undefined
alert(typeof testVar); //shows undefined

Why is null an object and what's the difference between null and undefined?

(name is undefined)

You: What is name? (*)

JavaScript: name? What's a name? I don't know what you're talking about. You haven't ever mentioned any name before. Are you seeing some other scripting language on the (client-)side?

name = null;

You: What is name?

JavaScript: I don't know.

In short; undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null is where the thing is known to exist, but it's not known what the value is.

One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, i.e.

name = false;

You: What is name?

JavaScript: Boolean false.

name = '';

You: What is name?

JavaScript: Empty string


*: name in this context is meant as a variable which has never been defined. It could be any undefined variable, however, name is a property of just about any HTML form element. It goes way, way back and was instituted well before id. It is useful because ids must be unique but names do not have to be.

What is the difference between null and undefined in JavaScript?

undefined means a variable has been declared but has not yet been assigned a value :

var testVar;
alert(testVar); //shows undefined
alert(typeof testVar); //shows undefined

What is the difference between Null, NaN and undefined in JavaScript?

NaN: Not a number: As the name implies, it is used to denote that the value of an object is not a number. There are many ways that you can generate this error, one being invalid math opertaions such as 0/0 or sqrt(-1)

undefined: It means that the object doesn't have any value, therefore undefined. This occurs when you create a variable and don't assign a value to it.

null: It means that the object is empty and isn't pointing to any memory address.

Typescript - What is the difference between null and undefined?

This post explains the differences very well. They are the same in TypeScript as in JavaScript.

As for what you should use: You may define that on your own. You may use either, just be aware of the differences and it might make sense to be consistent.

The TypeScript coding style guide for the TypeScript source code (not an official "how to use TypeScript" guide) states that you should always use undefined and not null: Typescript Project Styleguide.

difference between null and undefined in JavaScript?

You need to use the identity operator ===, not the equality operator ==. With this change, your code works as expected:

alert(a===null);      // true
alert(a===undefined); // false
alert(b===null); // false
alert(b===undefined); // true

The reason the equality operator fails in this case is because it attempts to do a type conversion. undefined is of type undefined, and null is of type object; in attempting to compare the two, Javascript converts both to false, which is why it ends up considering them equal. On the other hand, the identity operator doesn't do a type conversion, and requires the types to be equal to conclude equality.

Edit Thanks to @user1600680 for pointing out, the above isn't quite correct; the ECMAScript specification defines the null-to-undefined as special case, and equal. There's no intermediate conversion to false.



A simpler example of type conversion is number-to-string:

console.log(5 == "5");    // true
console.log(5 === "5"); // false

The above answer has a good quote from Douglas Crockford's Javascript: The Good Parts:

[The "==" operator does] the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable.

If you don't believe that the rules are complicated and unmemorable, a quick look at those rules
will disabuse you of that notion.

Is there any function difference between using != null and != undefined in javascript?

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)

Sample Image

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;

Sample Image

What reason is there to use null instead of undefined in JavaScript?

null and undefined are essentially two different values that mean the same thing. The only difference is in the conventions of how you use them in your system. As some have mentioned, some people use null for meaning "no object" where you might sometimes get an object while undefined means that no object was expected (or that there was an error). My problem with that is its completely arbitrary, and totally unnecessary.

That said, there is one major difference - variables that aren't initialized (including function parameters where no argument was passed, among other things) are always undefined.

Which is why in my code I never use null unless something I don't control returns null (regex matching for example). The beauty of this is it simplifies things a lot. I never have to check if x === undefined || x === null, I can just check x === undefined. And if you're in the habit of using == or simply stuff like if(x) ... , stop it.

!x will evaluate to true for an empty string, 0, null, NaN - i.e. things you probably don't want. If you want to write javascript that isn't awful, always use triple equals === and never use null (use undefined instead). It'll make your life way easier.

what difference is there between undefined and null?

undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value.
Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

reference: http://www.ajaymatharu.com/javascript-difference-between-undefined-and-null/



Related Topics



Leave a reply



Submit