Special values in JavaScript

In JavaScript, as in other programming languages, there are keywords for some special values. Here they are: undefined, null, true, false, NaN, Infinity, -Infinity.

Undefined value

The value undefined denotes uncertainty. A variable will have such a value if we declared it, but did not write the value to it:

let a; alert(a); // shows undefined

This value can be assigned explicitly to a variable :

let a = undefined; alert(a); // shows undefined

Declare a variable without assigning a value to it. Display the value of the created variable on the screen.

Null value

The value null means "nothing". For example, we can assign such a value to a variable to indicate that there is nothing there:

let a = null; alert(a); // shows null

Set the variable to null. Print this variable to the screen.

True and false values

The values true and false are used for things that require two answers - yes or no.

Set the variable to true. Display this variable on the screen.

Set the variable to false. Display this variable on the screen.

NaN value

The value NaN means Not-A-Number. This value is obtained when you try to do mathematical operations not with numbers, but with something else. For example, an attempt to multiply a string by a number will give such a value:

alert('abc' * 3); // shows NaN

Any operations with NaN still lead to the NaN result:

alert(NaN + 1); // shows NaN

Make two strings. Multiply them over each other. Print the result of the multiplication on the screen.

Infinity and -Infinity values

Such values as Infinity and -Infinity are obtained if some number is divided by zero. JavaScript does not throw an error, in such a case, as in other programming languages, but returns these values:

alert( 1 / 0); // shows Infinity alert(-1 / 0); // shows -Infinity

Divide the number 10 by 0. Display the result on the screen.

Divide the number -10 by 0. Display the result on the screen.

enru