JavaScript provides different data types to store and work with values. These are grouped into two main categories:
string, number, bigint, boolean, undefined, symbol, nullobject (including arrays, functions, and other complex structures)Understanding data types is essential for storing values correctly, avoiding bugs, and using memory efficiently.
typeof operator helps you check the data type of a value at runtime.null is a special value that represents “no value” or “empty”, but typeof null returns "object" due to a historical bug in JavaScript.You can declare variables using let, const, or var (modern code usually prefers let and const):
"Hello" or 'Hello'.n at the end.true or false.
// String
let name = "Alice";
// Number
let age = 25;
// BigInt
let bigNumber = 1234567890123456789012345678901234567890n;
// Boolean
let isStudent = true;
// Undefined
let score;
console.log(score); // undefined
// Null
let data = null;
// Symbol
let id = Symbol("unique");
// Object
let person = {
name: "John",
age: 30
};
// Array
let colors = ["red", "green", "blue"];
// Function
function greet() {
return "Hello!";
}
The typeof operator returns a string describing the data type of a value.
console.log(typeof "hello"); // "string"
console.log(typeof 123); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (historical bug)
console.log(typeof []); // "object"
console.log(typeof {}); // "object"
console.log(typeof function(){}); // "function"
If you run the typeof example in the browser console or a JS environment, you will see:
typeof "hello" → "string"typeof 123 → "number"typeof true → "boolean"typeof undefined → "undefined"typeof null → "object" (this is a long-known quirk in JavaScript)typeof [] and typeof {} → "object" (arrays are objects)typeof function(){} → "function" (a special subtype of object)Similarly, in the primitive example, logging score before assigning a value prints undefined, which means the variable exists but does not hold any value yet.
typeof to quickly check the type of a variable while debugging.null is an “empty” value but typeof null returns "object" due to a historical bug.Array.isArray() instead of typeof to detect arrays.const for values that do not change and let for values that will change.typeof results.typeof on a function, array, object, number, and null. Observe the results.Array.isArray() to check whether a variable is an array and compare it to using typeof.