Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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

I have come across all three values, and have understood them to be “there isn’t anything here” in the context I found them- but I was hoping for a more detailed explanation as to why they occur, as well as what they mean in different contexts (for example- against an array, vs a class or variable).

like image 352
Alex Mulchinock Avatar asked May 13 '18 21:05

Alex Mulchinock


People also ask

What is the difference between null and undefined JavaScript?

Definition: Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript. Undefined: It means the value does not exist in the compiler.

What is the difference between NaN and null?

NaN : means 0/0 -- Stands for Not a Number NA : is generally interpreted as a missing, does not exist NULL : is for empty object.

Is NaN null or undefined?

NaN values are generated when arithmetic operations result in undefined or unrepresentable values.

Is NaN and undefined the same?

In mathematics, zero divided by zero is undefined and is therefore represented by NaN in computing systems. The square root of a negative number is not a real number, and is therefore also represented by NaN in compliant computing systems. NaNs may also be used to represent missing values in computations.


1 Answers

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.

like image 79
Danyal Imran Avatar answered Oct 07 '22 23:10

Danyal Imran