JavaScript Data Types
Data types represent the different kinds of values we can use in JavaScript.
There are altogether 8 basic data types in JavaScript.
Data Type Description Example
String Textual data. 'hello', "hello world!", etc.
An integer or a floating-point
Number 3, 3.234, 3e-2, etc.
number.
BigInt An integer with arbitrary precision. 900719925124740999n, 1n, etc.
Boolean Any of two values: true or false. true and false
A data type whose variable is not
undefined let a;
initialized.
null Denotes a null value. let a = null;
A data type whose instances are
Symbol let value = Symbol('hello');
unique and immutable.
Object Key-value pairs of collection of data. let student = {name: "John"};
Note: JavaScript data types are divided into primitive and non-primitive types.
Primitive Data Types: They can hold a single simple
value. String, Number, BigInt, Boolean, undefined, null, and Symbol are primitive data types.
Non-Primitive Data Types: They can hold multiple values. Objects are non-primitive data types.
JavaScript String
A string represents textual data. It contains a sequence of characters. For example, "hello", "JavaScript",
etc.
In JavaScript, strings are surrounded by quotes:
Single quotes: 'Hello'
Double quotes: "Hello"
Backticks: `Hello`
For example,
// string enclosed within single quotes
let fruit = 'apple';
[Link](fruit)
// string enclosed within double quotes
let country = "USA";
[Link](country);
// string enclosed within backticks
let result = `fail`;
[Link](result);
Output
apple
USA
fail
In a string, we can either use single quotes or double quotes. However, it is recommended to use double
quotes.
Note: It is illegal to mismatch quotes in strings. For example, the strings 'hello" and "world' are enclosed
inside one single quote and one double quote, which results in an error.
JavaScript Number
In JavaScript, the number type represents numeric values (both integers and floating-point numbers).
Integers - Numeric values without any decimal parts. Example: 3, -74, etc.
Floating-Point - Numeric values with decimal parts. Example: 3.15, -1.3, etc.
// integer value
let integer_number = -3;
[Link](integer_number);
// floating-point value
let float_number = 3.15;
[Link](float_number);
Run Code
Output
-3
3.15
JavaScript BigInt
BigInt is a type of number that can represent very large or very small integers beyond the range of the
regular number data type.
Note: The regular number data type can handle values less than (2^53 - 1) and greater than -(2^53 - 1).
A BigInt number is created by appending n to the end of an integer. For example,
// BigInt value
let value1 = 900719925124740998n;
// add two big integers
let result1 = value1 + 1n;
[Link](result1); // "900719925124740999n"
let value2 = 900719925124740998n;
Output
900719925124740999n
TypeError: Cannot mix BigInt and other types, use explicit conversions
Note: BigInt was introduced in a newer version of JavaScript (ES11) and is not supported by many
browsers, including Safari.
You can't mix BigInt and number
You can't mix BigInt and number values.
In JavaScript, you can't mix BigInt and number values (for instance, by performing arithmetic operations
between them).
// BigInt value
let value = 900719925124740998n;;
// Error! BitInt and number cannot be added
let sum = value + 1;
[Link](sum);
Output
TypeError: Cannot mix BigInt and other types, use explicit conversions
JavaScript Boolean
A Boolean data can only have one of two values: true or false. For example,
let dataChecked = true;
[Link](dataChecked); // true
let valueCounted = false;
[Link](valueCounted); // false
JavaScript undefined
In JavaScript, undefined represents the absence of a value.
If a variable is declared but the value is not assigned, then the value of that variable will be undefined.
For example,
let name;
[Link](name); // undefined
It is also possible to explicitly assign undefined as a variable value. For example,
let name = undefined;
[Link](name); // undefined
Note: You should avoid explicitly assigning undefined to a variable. Usually, we assign null to variables to
indicate "unknown" or "empty" values.
JavaScript null
In JavaScript, null represents "no value" or "nothing." For example,
let number = null;
[Link](number); // null
Here, let number = null; indicates that the number variable is set to have no value.
JavaScript Symbol
A Symbol is a unique and primitive value. This data type was introduced in ES6.
When you create a Symbol, JavaScript guarantees that it is distinct from all other symbols, even if they
have the same descriptions. For example,
// two symbols with the same description
let value1 = Symbol("DBC");
let value2 = Symbol("DBC");
[Link](value1 === value2); // false
Here, we have used === to compare value1 and value2. It returns true if the two values are exactly the
same. Otherwise, it returns false.
Though both value1 and value2 contain "DBC", JavaScript treats them as different since they are of
the Symbol type. Hence, value1 === value2 returns false.
JavaScript Object
An Object holds data in the form of key-value pairs. For example,
let student = {
firstName: "John",
lastName: null,
class: 10
};
Here, we have created an object named student that contains key-value pairs:
Key Value
firstName "John"
lastName null
class 10