Cyrus Yip's blog

JavaScript Data Types

Cyrus Yip
Table of Contents

JavaScript has 8 data types:

  1. Undefined (undefined)
  2. Null (null)
  3. Boolean (true / false)
  4. Number
  5. BigInt
  6. String
  7. Symbol (unique value)
  8. Object

Object is the only mutable type. Other types are immutable and are called primitive types.

Undefined #

The Undefined type has only one value: undefined, which means a variable is declared but no value is assigned to it.

1
2
let name;
console.log(name); // undefined

undefined can be assigned to a variable but one should use null to represent emptiness.

1
2
3
4
let name = "Kate";
console.log(name) // Kate
name = undefined;
console.log(name); // undefined

Null #

The Null type has only one value: null, which means empty.

1
2
let name = null;
console.log(name); // null

Boolean #

The Boolean type has two values: true and false. The Boolean values are usually used in conditional operations.

1
2
3
4
5
6
console.log(1 > 2); // false
if (1 > 2) {
  console.log("One is greater than two."); // not executed
} else {
  console.log("One is less than two."); // executed
}

Number #

The Number type represents both integer and floating point numbers.

1
2
3
let num;
num = 10;
num = 10.1;

The Number type contains some special values: Infinity, -Infinity and NaN (Not a Number).

1
2
3
4
console.log(10 / 0); // Infinity
console.log(-10 / 0); // -Infinity
console.log(10 / 'a'); // NaN
console.log(NaN === NaN); // false

JavaScript can only safely store integers in the range of Number.MIN_SAFE_INTEGER -(253 βˆ’ 1) to Number.MAX_SAFE_INTEGER (253 βˆ’ 1).

1
2
3
4
console.log(Number.MAX_SAFE_INTEGER);     // 9007199254740991
console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 3); // 9007199254740994

BigInt #

The BigInt type can safely store integers beyond Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER. To create a BigInt value, append n to an integer or call the BigInt() function. Note that when BigInt() accept a number beyond the safe limits, the number will be rounded.

1
2
3
4
5
6
console.log(9007199254740991);           // 9007199254740991
console.log(9007199254740992);           // 9007199254740992
console.log(9007199254740993);           // 9007199254740992, number is rounded
console.log(9007199254740993n);          // 9007199254740993n
console.log(BigInt(9007199254740993));   // 9007199254740992n, number is rounded
console.log(BigInt("9007199254740993")); // 9007199254740993n

String #

The String type represents text.

1
2
3
4
5
6
const firstName = "Jane";
const lastName = "Doe";
const fullName = firstName + " " + lastName;
console.log(fullName); // Jane Doe
const selfIntroduction = `Hi, I am ${fullName}.`;
console.log(selfIntroduction); // Hi, I am Jane Doe.

Symbol #

The Symbol type represents a unique and immutable value. To create a Symbol value, call Symbol() function with an optional string as its description.

1
2
3
4
5
const sym1 = Symbol();
const sym2 = Symbol("foo");
const sym3 = Symbol("foo");
console.log(sym1 === sym2); // false
console.log(sym2 === sym3); // false

Object #

Object is a collection of key-value pairs, similar to a dictionary.

1
2
3
4
5
const englishDictionary = {
  article: "A piece of writing",
  happy: "Feeling or showing pleasure",
  water: "Clear and tasteless liquid, essential for life"
}

References #

Tags: