JavaScript has two main categories of data types: primitives and objects.
A primitive (or a primitive value or a primitive data type), as described in the JavaScript documentation, is the data that is not an object and has no methods.
JavaScript is a dynamically typed language, meaning variables are not bound to any specific data type. Instead, the type is determined at runtime.
There are 6 primitive data types in JavaScript: Boolean, Number, String, Null, Undefined, Symbol (introduced in ES6)
From ES2020, there's also a 7th primitive data type:
BigInt (for handling arbitrarily large integers)
1. Boolean
Boolean is a logical data type which can only have two values: true or false; It is a YES-NO switch; Logical operation results in a boolean value;
let isActive = true;
console.log(typeof isActive); // "boolean"
Common Use Cases
- Controlling the flow of an application
- Logical conditions in if statements
2. Number
Number is a numeric data type in the double-precision 64-bit floating point format. In JS, number represents both integers and floating points.
let intNum = 42; // Integer
let floatNum = 3.14; // Floating-point number
let expNum = 5e3; // Exponential notation (5000)
let negativeNum = -10; // Negative number
console.log(typeof intNum); // "number"
Special Number Values
- Infinity (positive infinity)
- -Infinity (negative infinity)
- NaN (Not a Number, usually from invalid calculations)
Common Use Cases
- Mathematical calculations
- Representing numeric data
3. String
A string is a sequence of characters used to represent text. In JS, a string is inside of double or single quotes. ES6 also introduced template literals or template strings.
let singleQuoteStr = 'Hello';
let doubleQuoteStr = "World";
let templateStr = `Hello, ${doubleQuoteStr}!`;
console.log(typeof singleQuoteStr); // "string"
Common Use Cases
- Storing and manipulating text data
- Dynamically constructing strings using template literals
4. Null
null represents "nothing" or an intentional absence of value.
It is often used to indicate that a variable should have no value.
Note: The typeof null is 'object'. This is a known bug in JavaScript that has existed since its early days but was never fixed for backward compatibility.(why null is object)
let emptyValue = null;
console.log(typeof emptyValue); // "object"
Common Use Cases
- Explicitly indicating the absence of a value
5. Undefined
Undefined is a data type that it stands for a value that is not defined;
let notAssigned;
console.log(notAssigned); // undefined
console.log(typeof notAssigned); // "undefined"
Common use case:
- Check if a variable is assigned with a value
6. Symbol (ES6)
Symbol is a unique and immutable primitive value introduced in ES6.
It is commonly used as unique property keys.
let sym1 = Symbol("id");
let sym2 = Symbol("id");
console.log(sym1 === sym2); // false (each Symbol is unique)
console.log(typeof sym1); // "symbol"
Common Use Cases
- Creating unique object keys to prevent naming conflicts
- Implementing privacy in objects
7. BigInt (ES2020)
Introduced in ES2020, BigInt allows you to store very large integers beyond Number.MAX_SAFE_INTEGER.
let bigNumber = 9007199254740991n; // The 'n' at the end makes it a BigInt
console.log(typeof bigNumber); // "bigint"
Common Use Cases
- Storing cryptographic keys
- Working with extremely large numbers
References:
A quick overview of JavaScript symbols
Top comments (0)