Limited Offer
60% OFF on all plans!

Operators in Node.js

Complete guide to JavaScript operators: arithmetic, assignment, comparison, logical, bitwise, string, conditional, typeof, instanceof, nullish coalescing, optional chaining, and operator precedence.

Introduction to Operators

Operators are symbols or keywords that perform operations on values (operands). They are the building blocks of expressions and logic in JavaScript.

1. What are Operators?

Operators work on operands.

Arithmetic Operators

Arithmetic operators are used to perform basic math operations.

1. + - * / %

Basic arithmetic operators.

2. Increment and Decrement

++ and -- operators.

Assignment Operators

Assignment operators assign values to variables and can combine with arithmetic.

1. = Assignment

Assign a value.

2. Compound Assignment

+= -= *= /=

Comparison Operators

Comparison operators return boolean values.

1. == vs ===

Loose vs strict equality.

2. < > <= >=

Relational comparisons.

Logical Operators

Logical operators are commonly used in conditions.

1. && || !

Logical AND, OR, NOT.

2. Short-circuit Behavior

Logical operators return values.

String Operators

The + operator can concatenate strings.

1. String Concatenation

Use + with strings.

Conditional (Ternary) Operator

The ternary operator is a compact alternative to if-else.

1. ? : Operator

condition ? value1 : value2

Type Operators

JavaScript provides operators to check types.

1. typeof

Check primitive types.

2. instanceof

Check object type.

Nullish & Optional Operators

Modern JavaScript provides operators to safely access values.

1. Nullish Coalescing (??)

Fallback only for null or undefined.

2. Optional Chaining (?.)

Safe property access.

Bitwise Operators

Bitwise operators work on 32-bit signed integers. JavaScript converts numbers to binary internally, applies the operation bit by bit, and converts the result back to a number.

1. Binary Representation (Important Foundation)

Understanding bits is mandatory for bitwise operators.

2. Bitwise AND (&)

Returns 1 only if both bits are 1.

3. Bitwise OR (|)

Returns 1 if at least one bit is 1.

4. Bitwise XOR (^)

Returns 1 if bits are different.

5. Bitwise NOT (~)

Inverts all bits.

6. Left Shift (<<)

Shifts bits to the left (multiplies by powers of 2).

7. Right Shift (>>)

Shifts bits to the right (divides by powers of 2).

8. Unsigned Right Shift (>>>)

Shifts bits right and fills with zeros.

9. Practical Use Cases

Where bitwise operators are actually used.

Operator Precedence

Precedence determines how expressions are evaluated.

1. Precedence Example

Use parentheses to control order.

2. Operator Precedence Table

Higher precedence operators are evaluated first. When in doubt, use parentheses.

Spread Operator

The spread operator (...) expands elements of arrays or properties of objects. It is heavily used for copying, merging, and inline composition.

1. Spread with Arrays

Expand array elements.

2. Spread with Objects

Expand object properties.

Operator Precedence

Operator precedence determines how expressions are grouped and evaluated when parentheses are not used.

1. Basic Precedence Rules

Some operators run before others.

2. Operator Precedence Table

Higher precedence operators are evaluated first.

Conditional Object Fields (Spread + Ternary)

JavaScript allows building objects dynamically using spread syntax combined with conditional (ternary) expressions. This is extremely useful in APIs and configuration objects.

1. Conditional Field Addition

Add fields only when condition is true.

2. Conditional + Destructuring Together

Destructure and conditionally rebuild.