Toolsnip

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

Javascript Interview Questions and Answers

Short Answer

'null', 'undefined', and 'NaN' are all values used to represent different types of absence or error in JavaScript.

Detailed Answer

'null' is a value that represents the intentional absence of any object value.

'undefined' is a value that indicates the absence of a defined value, such as a variable that has not been initialized.

'NaN' stands for 'Not a Number' and is a value returned when a mathematical operation cannot be performed, such as dividing by zero or converting a string to a number that is not a valid number.

'null' and 'undefined' are primitive values, while 'NaN' is a special value of the number type.

Overall, 'null' is used to represent the absence of an object value, 'undefined' is used to represent the absence of a defined value, and 'NaN' is used to represent an error in a mathematical operation.