JavaScript ยท Chapter 8 of 55

JavaScript Operators

Operators perform actions on values, called operands. JavaScript groups operators into categories: arithmetic, assignment, comparison, logical, and more.

Understanding operator precedence โ€” the order operations are evaluated โ€” helps avoid unexpected results in complex expressions.

Categories

Arithmetic (`+ - * / % **`), assignment (`= += -=`), comparison (`== === < >`), logical (`&& || !`), and the ternary operator (`? :`) cover most needs.

Operator precedence

Multiplication and division run before addition and subtraction, similar to math class. Parentheses `()` override the default order.

Example 1 (javascript)
let result = 2 + 3 * 4;
console.log(result);
Output
14

Multiplication happens before addition.

Example 2 (javascript)
let age = 20;
let canVote = age >= 18 ? "Yes" : "No";
console.log(canVote);
Output
Yes

The ternary operator is a compact if/else.

Key points

  • Operators act on operands to produce a value.
  • Precedence determines evaluation order; use () to be explicit.
  • The ternary operator `cond ? a : b` is a compact conditional.
  • Logical operators `&&`, `||`, `!` combine boolean expressions.
๐Ÿ’ก Note: When in doubt about precedence, add parentheses โ€” clarity beats cleverness.

๐Ÿ“ Quick Quiz

1. What is the result of `2 + 3 * 4`?

2. The ternary operator syntax is:

3. Which combines two booleans with AND logic?