Number Type
// Number Representation
let integer = 42; // Whole number
let decimal = 3.14; // Floating-point number
let scientific = 5e3; // Scientific notation (5000)
let hexadecimal = 0xFF; // Hexadecimal (255)
let binary = 0b1010; // Binary representation (10)
Infinity, Infinity, NaNNaN is a unique value meaning "Not a Number"NaN is the only JavaScript value that is not equal to itselfNumber.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER define safe integer rangesString Type
// String Immutability Demonstration
let str = "immutable";
str[0] = 'I'; // This won't change the string
Boolean Type
Similar to boolean in any programming language, it has two values true and false
All values except false, 0, 0n, -0, “”, null, undefined, and NaN are truthy values.
Undefined Type
let string;
console.log(string); // undefined
undefined represents a variable that has been declared but not assigned a value, automatically set by JavaScript. It is diffrent from null.
Null Type
null is a special value in JavaScript that developers deliberately use to show a variable is intentionally empty or has no value assigned to it.
typeof null returns "object" (a historical bug in JavaScript)Symbol Type ( ES6 )
In JavaScript, a Symbol is a unique and immutable primitive type that creates guaranteed distinct values, primarily used to create non-string property keys and prevent property name collisions in objects
// guaranteed distinct values
let sym1 = Symbol("description");
let sym2 = Symbol("description");
console.log(sym1 === sym2); // false
// use case in objects to get unique keys
const uniqueKey = Symbol('description');
const obj = {
[uniqueKey]: 'This is a unique property'
};
BigInt Type ( ES2020 )
let bigNum = 1234567890123456789012345678901234567890n;
let bigIntValue = BigInt(42);
The typeof operator we discussed earlier is used for type checking in JavaScript. It allows you to determine the type of a given variable
Type Coercion in JavaScript is the process of converting one data type to another, either automatically (implicit) or manually (explicit).
Type conversion methods are explicit ways to convert a value from one type to another. Some common methods include:
String() – Converts a value to a string.Number() – Converts a value to a number.