JavaScript has two main types of values: Primitive and Reference (Non-Primitive).
Primitive Types
Primitive values are immutable and stored directly in memory. There are 7 primitive types:
- String: Represents a sequence of characters.
const name = "John";
- Number: Represents both integer and floating-point numbers.
const score = 100;
const scoreValue = 100.3;
- Boolean: Represents
true
orfalse
values.const isLoggedIn = false;
- null: Represents the intentional absence of any object value.
const outsideTemp = null;
- undefined: Represents a variable that has been declared but not yet assigned a value.
let userEmail;
- Symbol: Represents a unique and immutable identifier, often used for object property keys.
const id = Symbol('123');
const anotherId = Symbol('123');
console.log(id === anotherId); // false
Symbols are always unique, even if they have the same description.
- BigInt: Represents whole numbers larger than the safe integer limit (
Number.MAX_SAFE_INTEGER
).// const bigNumber = 3456543576654356754n;
Reference Types (Non-Primitive)
Reference types are objects that are stored as a reference in memory. When you work with reference types, you're dealing with a reference to the object, not the actual object itself.
- Array: An ordered collection of values.
const heros = ["shaktiman", "naagraj", "doga"];
- Object: A collection of key-value pairs.
let myObj = { name: "hitesh", age: 22 };
- Function: A block of code designed to perform a particular task.
const myFunction = function() { console.log("Hello world"); };
Type Checking
Use typeof
to check the type of a variable.
console.log(typeof anotherId); // "symbol"