Skip to content

Operators and Expressions

Operators are symbols used to perform operations on values (operands).

An expression is a combination of values, variables, and operators that produces a result.

let result = 10 + 5;
  • + → operator
  • 10, 5 → operands
  • 10 + 5 → expression

graph LR A[Operands] --> B[Operator] B --> C[Evaluation] C --> D[Result]

Arithmetic

Perform mathematical operations like addition, subtraction, etc.

Assignment

Assign values to variables.

Comparison

Compare values and return boolean results.

Logical

Combine multiple conditions.


  1. Addition (+) Adds two values Example: 10 + 5 = 15

  2. Subtraction (-) Example: 10 - 5 = 5

  3. Multiplication (*) Example: 10 * 5 = 50

  4. Division (/) Example: 10 / 5 = 2

  5. Modulus (%) Returns remainder Example: 10 % 3 = 1

  6. Exponentiation ()** Example: 2 ** 3 = 8


let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a % b); // 1

Used to assign values.

let x = 10;
x += 5; // x = x + 5
x -= 2;
x *= 3;
x /= 2;

graph TD A[Variable] --> B[Operator] B --> C[New Value]

Return true or false.

10 > 5; // true
10 < 5; // false
10 >= 10; // true
10 <= 5; // false

"5" + 2; // "52"
"5" - 2; // 3


Used to combine conditions.

  1. AND (&&) True if both conditions are true

  2. OR (||) True if at least one condition is true

  3. NOT (!) Reverses boolean value


let age = 20;
console.log(age > 18 && age < 30); // true
console.log(age > 18 || age < 10); // true
console.log(!(age > 18)); // false

JavaScript stops evaluation early.

true || console.log("Won't run");
false && console.log("Won't run");

graph LR A[Condition 1] -->|Short Circuit| B[Skip Next]

Operate on a single operand.

let x = 5;
x++; // increment
x--; // decrement
typeof x; // type check

Determines execution order.

let result = 10 + 5 * 2; // 20, not 30

graph TD A[Multiplication] --> B[Addition] B --> C[Final Result]