JS Data Types
In this page:
let text = "string";
let number = 10;
let flag = true;
let list = [item1, item2];
let object = { key: value };
String, Number, and Boolean
string represents text wrapped in quotes, number represents both whole and decimal numbers using a single type, and boolean represents only true or false, the three most commonly used data types in everyday JavaScript.
Example: String, Number, and Boolean
// Declare the variable `name`, set to "Alice"
// Declare the variable `name`, set to "Alice"
let name = "Alice";
// Declare the variable `age`, set to `30`
// Declare the variable `age`, set to `30`
let age = 30;
// Declare the variable `isActive`, set to `true`
// Declare the variable `isActive`, set to `true`
let isActive = true;
// Print `typeof name, typeof age, typeof isActive` to the console
// Print `typeof name, typeof age, typeof isActive` to the console
console.log(typeof name, typeof age, typeof isActive);
undefined and null
undefined means a variable has been declared but never given a value, while null is an intentional value developers assign to represent nothing or 'empty on purpose', they represent different concepts despite both meaning an absence of a real value.
Example: undefined and null
let notSet;
let empty = null;
console.log(notSet); // undefined
console.log(empty); // null
bigint and symbol
bigint represents whole numbers larger than the regular number type can safely handle, written with an n suffix, while symbol creates a guaranteed unique value, often used as a special, collision-free property key.
Example: bigint and symbol
// Declare the variable `big`, set to `12345678901234567890n`
// Declare the variable `big`, set to `12345678901234567890n`
let big = 12345678901234567890n;
// Declare the variable `id`, set to `Symbol("id")`
// Declare the variable `id`, set to `Symbol("id")`
let id = Symbol("id");
// Print `typeof big, typeof id` to the console
// Print `typeof big, typeof id` to the console
console.log(typeof big, typeof id);
The object Type
object is the only non-primitive type, used to group related data and functionality together, and it covers not just plain objects but also arrays, functions, and dates, all of which report as object or function with typeof.
Example: The object Type
// Declare the variable `user`, set to `{ name: "Sam" }`
// Declare the variable `user`, set to `{ name: "Sam" }`
let user = { name: "Sam" };
// Declare the variable `list`, set to `[1, 2, 3]`
// Declare the variable `list`, set to `[1, 2, 3]`
let list = [1, 2, 3];
// Print `typeof user, typeof list` to the console
// Print `typeof user, typeof list` to the console
console.log(typeof user, typeof list);
Dynamic Typing
JavaScript is dynamically typed, meaning a variable's type is not fixed when declared, it's determined by whatever value currently sits inside it, and that value, along with its type, can change if the variable is reassigned.
Example: Dynamic Typing
let value = 5;
console.log(typeof value); // "number"
value = "now a string";
console.log(typeof value); // "string"
- Assuming null and undefined are the same thing, they are related but distinct values with different intended meanings.
- Forgetting that JavaScript is dynamically typed, meaning a variable's type can change if you reassign it to a different kind of value.
- Using typeof to check for null, since typeof null incorrectly returns object due to a long-standing language quirk.
- JavaScript's primitive types are string, number, bigint, boolean, undefined, null, and symbol, plus the non-primitive object type.
- typeof is the standard way to check a value's type at runtime.
- JavaScript is dynamically typed, a variable's type is determined by its current value and can change over time.
All core JavaScript data types are part of the language standard and behave identically across every modern browser; BigInt is supported in all current browser versions.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: