Operator
A symbol or keyword in JavaScript that performs operations on values and variables, such as arithmetic, comparison, logical, or assignment operations.
Overview
Operators are fundamental building blocks in JavaScript that allow you to perform operations on data. JavaScript has several types of operators including arithmetic (+, -, *, /), comparison (===, !==, <, >), logical (&&, ||, !), assignment (=, +=, -=), and more specialized operators like the ternary operator and optional chaining.
Example
javascript// Arithmetic operators const sum = 10 + 5; // 15 const difference = 10 - 5; // 5 const product = 10 * 5; // 50 const quotient = 10 / 5; // 2 const remainder = 10 % 3; // 1 const power = 2 ** 3; // 8 // Comparison operators const isEqual = 5 === 5; // true (strict equality) const isNotEqual = 5 !== 3; // true const isGreater = 10 > 5; // true const isLessOrEqual = 5 <= 5; // true // Logical operators const and = true && false; // false const or = true || false; // true const not = !true; // false // Assignment operators let x = 10; x += 5; // x = x + 5 (15) x -= 3; // x = x - 3 (12) x *= 2; // x = x * 2 (24) // Ternary operator const age = 20; const status = age >= 18 ? 'adult' : 'minor'; // Optional chaining const user = { address: { city: 'NYC' } }; const city = user?.address?.city; // 'NYC' const zip = user?.address?.zip; // undefined (no error) // Nullish coalescing const value = null ?? 'default'; // 'default' const count = 0 ?? 'default'; // 0 (not 'default') // Spread operator const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5] // typeof operator typeof 'hello'; // 'string' typeof 42; // 'number' typeof true; // 'boolean'
Key Categories
- Arithmetic:
+,-,*,/,%,** - Comparison:
===,!==,<,>,<=,>= - Logical:
&&,||,! - Assignment:
=,+=,-=,*=,/= - Other:
?.(optional chaining),??(nullish coalescing),?:(ternary)
Key Points
- Symbols that perform operations on values
- Different types for different purposes
- Precedence determines evaluation order
- Some operators short-circuit (
&&,||) - Modern operators:
??,?.
Learn More
- JavaScript - JavaScript language
- Spread Operator - Spread syntax
- MDN: Expressions and operators