JavaScript Operators
In JavaScript, operators are special symbols that perform calculations, assignments, or comparisons on operands (variables and values). Understanding them is crucial for writing any logic.
Arithmetic Operators
Used to perform mathematical calculations. Standard math rules (PEMDAS) apply. The basic ones are +, -, *, and /. A very useful addition is the Modulo % which returns the remainder of a division.
Assignment Operators
Used to assign values to variables. The most basic is =. You can combine math and assignment with shorthand operators like += or *=.
Note: The single = is ONLY for assignment, never for comparison!
Comparison & Logical Operators
Comparison operators evaluate to a boolean (true or false). Always prefer Strict Equality === over loose equality ==, because strict checks both value and type, avoiding weird JS type coercion bugs.
Logical operators like AND &&, OR ||, and NOT ! allow you to chain multiple conditions together to build complex if-statements.
Advanced: The Ternary Operator+
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if...else statement.
