JavaScript Data Types: Primitive & Non-Primitive
What is data type ?
A data type in JavaScript defines the type of value a variable can hold. It determines how the data is stored and manipulated in memory.
Why is it Used?
- Ensures proper operations on values
- Helps in memory management
- Avoids runtime errors
JavaScript has eight data types, categorized as following two types:
Primitive
Primitive data types are stored directly in the memory and are immutable, meaning their values cannot be changed after creation.
let str = "Hello";
str[0] = "h"; // No effect, strings are immutable
console.log(str); // Output: "Hello"
let newStr = str.toUpperCase(); // Creates a new string
console.log(newStr); // Output: "HELLO"
primitive data types in JavaScript are typically stored in the stack memory because they have fixed sizes and are immutable. However, if a primitive is part of an object (which is stored in the heap), its value is stored within the object's memory space in the heap.
1. Number
The Number data type in JavaScript is a primitive type used to store numeric values, including integers and floating-point numbers. Example : 7, 0, -7, 2.8, -8.5, Infinity, -Infinity, NaN
// All of the following variables contains Number data-type
const a = 7
const b = 0
const c = -7
const d = 2.8
const e = -8.5
const f = Infinity
const g = -Infinity
const h = NaN
// Copy behaviour
let num1 = 7;
let num2 = num1; // Copy of value, not reference
num1 = 10;
console.log(num2); // 7 (remains unchanged)
// Stored in stack as a fixed-size value.
// Copying a number creates a separate memory space.
Property | Value | Exceeding Effect |
---|---|---|
Number.MAX_VALUE | 1.7976931348623157e+308 | Infinity |
Number.MIN_VALUE | 5e-324 | 0 |
Number.MAX_SAFE_INTEGER | 9007199254740991, (2^53 - 1) | Precision Loss |
Number.MIN_SAFE_INTEGER | -9007199254740991, -(2^53 - 1) | Precision Loss |
2. BigInt
BigInt is a primitive data type in JavaScript that allows the representation of integers larger than Number.MAX_SAFE_INTEGER (2^53 - 1) without losing precision. It is mainly used for arbitrary-precision arithmetic, cryptography, and precise financial calculations.
A BigInt is created by adding n at the end of an integer or by using the BigInt() function.
let big1 = 123456789012345678901234567890n // Using "n" suffix
let big2 = BigInt(123456789012345678901234567890n) // Using BigInt() constructor
- The exact storage format depends on the JavaScript engine
- It is not stored directly in the stack due to its dynamic size.The stack contains a reference to the BigInt stored in the heap.
- BigInt has no predefined max/min limit, so it will keep growing as long as memory allows.if it grows too large then Memory overflow/Performance issues/Crashes may occure.
- Number, which follows IEEE 754 (64-bit floating-point), BigInt uses a variable-length representation for storing large numbers.
let big1 = BigInt(123456789012345678901234567890); // => 123456789012345677877719597056n
let big2 = BigInt("123456789012345678901234567890"); // => 123456789012345678901234567890n
Wait Wait Wait !!! , In above example values are looking similar but why they are storing different value in variable ??? In variable big1 in above example a Number data-type is passed to BigInt, if the number exceeds the MAX_SAFE_INTEGER then javascript rounds the number which leads to Precision Loss
(If you still havn't understood bigint, then assume bigint a only numeric string. operations can be performs between two bigint)
Best Practice : Always use a string when working with very large numbers in BigInt()
3. String
A string is a sequence of characters used to represent text in programming. In JavaScript (and many other languages), a string is immutable, meaning it cannot be changed after it's created. We can create a string using :
- Single quotes → 'Hello'
- Double quotes → "Hello"
- Backticks (template literals) → `Hello`
name1 = 'John' // Using single quotes
name2 = " Doe" // Using double quotes
name3 = `Bob` // Using backticks
name4 = 'alice'
name5 = `${name4} carlos` // interpolation Output: alice carlos
- Strings are stored in memory as a sequence of Unicode characters.
- Strings smaller than a certain size are stored into stack and bigger are stored into heap, the size limit depends upon browser and system memory
- Since string is immutable, if you want to modify a string then you have to create a new one instead of modifying the existing one.
let str = "Cyber";
str[0] = "X";
console.log(str); // "Cyber" (No change, because strings are immutable)
let str1 = "Hello";
let str2 = str1; // str2 points to the same "Hello" in memory
str1 = "World"; // A new "World" string is created; str2 is still "Hello"
4. Boolean
A Boolean is a data type that can only have two possible values :
- false : In JavaScript, the following values are treated as falsy - false, 0, -0, 0n, "", null, undefined, NaN
- true : Everything except the falsy values is truthy.
if ("hello") console.log("hello is true") // gives output
if (0) console.log("0 is false") // no output
- Stored in the stack as a single bit or byte (depending on optimization).
5. Undefined
undefined is a primitive data type in JavaScript that represents a variable that has been declared but not assigned a value.
- undefined means not assigned a value. JavaScript automatically assigns undefined to uninitialized variables.
- Objects & arrays return undefined for missing properties or indexes.
- Functions return undefined if no return statement is used.
let a
console.log(a) // Output: undefined
let ary = [1,2,5]
console.log(ary[5]) // Output: undefined
6. null
null is a primitive data type in JavaScript that represents intentional absence of a value. It is explicitly (& intentionally) set by the programmer
let x = null;
console.log(x); // Output: null
- BUG : JavaScript incorrectly treats null as an object due to a bug in early implementations.
7. Symbol
Comming soon .....