JavaScript has several “special” values that behave differently from regular values. These special values are used to represent unique conditions, such as undefined states, invalid numbers, or extreme numerical values. Below are the main special values in JavaScript:
1. undefined
The undefined
value indicates that a variable has been declared but has not yet been assigned a value.
Characteristics:
- It’s a primitive value.
- It’s automatically assigned to uninitialized variables.
- Functions return
undefined
if they do not explicitly return a value.
Example:
let a;
console.log(a); // Output: undefined
2. null
The null
value is a special value that represents the intentional absence of any object value. It is often used to signify “no value” or “empty.”
Characteristics:
- It’s a primitive value.
null
is an object type, but it’s considered a primitive value.- It’s explicitly set by the programmer to indicate “no value.”
Example:
let b = null;
console.log(b); // Output: null
3. NaN
(Not-a-Number)
NaN
is a special value that indicates a value that is “not a legal number.” It is usually the result of invalid or undefined mathematical operations.
Characteristics:
NaN
is of the number type.NaN
is not equal to any value, including itself (e.g.,NaN !== NaN
).- To check if a value is
NaN
, useisNaN()
orNumber.isNaN()
.
Example:
let c = 0 / 0;
console.log(c); // Output: NaN
console.log(NaN === NaN); // Output: false
console.log(isNaN(c)); // Output: true
4. Infinity
and -Infinity
Infinity
represents the result of dividing a number by zero or a number that exceeds the largest possible value. -Infinity
is the result of a negative number divided by zero or a very large negative value.
Characteristics:
- Both
Infinity
and-Infinity
are of the number type. Infinity
is greater than any number, and-Infinity
is less than any number.
Example:
let d = 1 / 0;
console.log(d); // Output: Infinity
let e = -1 / 0;
console.log(e); // Output: -Infinity
5. 0
and -0
JavaScript has both positive zero (0
) and negative zero (-0
). Though they are mostly indistinguishable, they can produce different results in certain operations.
Characteristics:
- Both
0
and-0
are of the number type. 0 === -0
returnstrue
, but you can distinguish them using1 / 0
and1 / -0
.
Example:
console.log(0 === -0); // Output: true
console.log(1 / 0); // Output: Infinity
console.log(1 / -0); // Output: -Infinity
6. Symbol
Symbol
is a special primitive data type introduced in ES6 (ECMAScript 2015). It is used to create unique identifiers for object properties that are guaranteed to be unique.
Characteristics:
- It’s a primitive data type.
- Each
Symbol
value is unique, even if they have the same description. - Symbols are commonly used as keys in objects to avoid name collisions.
Example:
let sym1 = Symbol('description');
let sym2 = Symbol('description');
console.log(sym1 === sym2); // Output: false
7. BigInt
BigInt
is a special numeric type introduced in ES2020 to represent integers that are too large to be represented by the Number
type.
Characteristics:
- It can represent arbitrarily large integers.
- BigInt values are created by appending
n
to the end of an integer literal.
Example:
let bigIntValue = 1234567890123456789012345678901234567890n;
console.log(bigIntValue); // Output: 1234567890123456789012345678901234567890n
8. Boolean
Values (true
and false
)
JavaScript has two boolean values: true
and false
. These values are the result of logical operations and are used in conditional testing.
Characteristics:
- It’s a primitive data type.
- Booleans are returned by comparison operators (
<
,>
,===
, etc.) and logical operators (&&
,||
, etc.).
Example:
let isJavaScriptFun = true;
console.log(isJavaScriptFun); // Output: true
Summary of JavaScript Special Values:
Special Value | Description |
---|---|
undefined | A variable that has been declared but not assigned. |
null | Represents “no value” or “empty value.” |
NaN | Indicates an invalid or undefined number. |
Infinity | Represents a value greater than any finite number. |
-Infinity | Represents a value smaller than any finite number. |
0 and -0 | Positive and negative zero, treated mostly the same. |
Symbol | Represents a unique, immutable identifier. |
BigInt | Represents large integers beyond Number limitations. |
true , false | Boolean values used in logic operations. |
These special values help JavaScript handle unique situations like undefined data, extreme numerical values, and special object keys. Understanding how to use them effectively is crucial in avoiding common bugs and writing cleaner, more predictable code.