Operators and Expressions

Operators are special symbols or keywords that perform operations on one or more operands. Operands are values that operators act upon.

Expressions are combinations of variables, values, and operators that evaluate to a single value. Understanding how operators work and how expressions are evaluated is fundamental to programming.

Types of Operators

Operators can be broadly categorized based on the type of operation they perform:

1. Arithmetic Operators

These operators perform mathematical calculations.

Operator Description Example
+ Addition 5 + 3 = 8
- Subtraction 10 - 4 = 6
* Multiplication 6 * 7 = 42
/ Division 20 / 5 = 4
% Modulo (Remainder of division) 10 % 3 = 1
++ Increment (Adds 1 to the operand) let x = 5; x++; // x is now 6
-- Decrement (Subtracts 1 from the operand) let y = 10; y--; // y is now 9

2. Assignment Operators

These operators assign values to variables.

Operator Description Example
= Assigns the value of the right operand to the left operand. let a = 10; // a is now 10
+= Adds the right operand to the left operand and assigns the result to the left operand. let b = 5; b += 3; // b is now 8 (equivalent to b = b + 3)
-= Subtracts the right operand from the left operand and assigns the result to the left operand. let c = 15; c -= 5; // c is now 10 (equivalent to c = c - 5)
*= Multiplies the left operand by the right operand and assigns the result to the left operand. let d = 4; d *= 2; // d is now 8 (equivalent to d = d * 2)
/= Divides the left operand by the right operand and assigns the result to the left operand. let e = 20; e /= 4; // e is now 5 (equivalent to e = e / 4)
%= Performs modulo operation and assigns the result. let f = 10; f %= 3; // f is now 1 (equivalent to f = f % 3)

3. Comparison (Relational) Operators

These operators compare two values and return a boolean value (true or false).

Operator Description Example
== Equal to (value) 5 == 5 // true
!= Not equal to (value) 5 != 3 // true
=== Strictly equal to (value and type) 5 === '5' // false
!== Strictly not equal to (value and type) 5 !== '5' // true
> Greater than 10 > 5 // true
< Less than 5 < 10 // true
>= Greater than or equal to 5 >= 5 // true
<= Less than or equal to 5 <= 10 // true
Note: It is generally recommended to use strict comparison operators (=== and !==) to avoid unexpected type coercion issues.

4. Logical Operators

These operators combine boolean expressions.

Operator Description Example
&& Logical AND (Returns true if both operands are true) (5 > 3) && (10 < 20) // true
|| Logical OR (Returns true if at least one operand is true) (5 > 10) || (10 < 20) // true
! Logical NOT (Reverses the boolean value of the operand) !(5 > 10) // true

5. Unary Operators

These operators operate on a single operand.

6. Ternary Conditional Operator

A shorthand for an if-else statement.

condition ? value_if_true : value_if_false;

Example:

let age = 18;
let status = (age >= 18) ? "Adult" : "Minor"; // status will be "Adult"

7. Bitwise Operators

These operators perform bit-by-bit operations on their operands.

Common bitwise operators include: & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Sign-propagating Right Shift), >>> (Zero-fill Right Shift).

8. Other Operators

Operator Precedence and Associativity

When an expression contains multiple operators, operator precedence determines the order in which operations are performed. For operators with the same precedence, associativity determines the order of evaluation (left-to-right or right-to-left).

Tip: Use parentheses () to explicitly control the order of evaluation and improve code readability. For example, (a + b) * c is different from a + (b * c).

Expressions

An expression is a fragment of code that produces a value. Expressions can be simple, like a literal value or a variable, or complex, involving multiple operators and function calls.

Examples of expressions:

Statements, on the other hand, perform an action. Expressions are often part of statements. For instance, in the statement let result = x + 5;, x + 5 is an expression that evaluates to a value, which is then assigned to the variable result.

Understanding operators and expressions is crucial for writing any meaningful code. Mastery of these concepts will allow you to perform calculations, make decisions, and manipulate data effectively.

Continue to the next section to explore control flow statements.