Syntax
let value = "text"; // string, number, boolean, etc.
typeof value; // "string"Examples
The Primitive Types
Examples of each primitive type and what typeof reports for it.
typeof "hello"; // "string"
typeof 42; // "number"
typeof 3.14; // "number" - no separate float type
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" - a famous long-standing JS quirk
typeof 10n; // "bigint"
typeof Symbol("id"); // "symbol"
typeof { a: 1 }; // "object"
typeof [1, 2, 3]; // "object" - arrays are objects too
typeof function(){}; // "function"Dynamic Typing
A variable's type can change as new values are assigned to it.
let value = "hello"; // string
value = 42; // now a number
value = true; // now a boolean
value = { a: 1 }; // now an object
// No errors - JavaScript allows this by designBest practices
- Use typeof to check a value's type at runtime, but remember typeof null is "object" due to a long-standing language quirk
- Use Array.isArray() rather than typeof to check specifically for arrays, since typeof reports both as "object"
- Prefer === over == to avoid unexpected type coercion during comparisons
- Consider TypeScript if your project would benefit from catching type mismatches before runtime
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.Assignment Operators
Assignment operators assign values to variables. Beyond the basic = operator, compound assignment operators combine an operation with assignment in one step, like += to add and assign. Logical assignment operators (&&=, ||=, ??=), introduced more recently, combine a logical check with assignment.
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.Assignment Operators
Assignment operators assign values to variables. Beyond the basic = operator, compound assignment operators combine an operation with assignment in one step, like += to add and assign. Logical assignment operators (&&=, ||=, ??=), introduced more recently, combine a logical check with assignment.