JavaScript Operators

JavaScript operators are special symbols used to perform operations on values and variables. These operations can range from simple arithmetic and assignment to comparison and logical evaluations.

Operators are categorized based on the type of operation they perform and the number of operands they take. An operand is the value or variable that the operator acts upon.

📊 Operator Categories

Arithmetic

Mathematical operations

+ - * / % **
Assignment

Assign values

= += -= *= /=
Comparison

Compare values

== === != !== > <
Logical

Combine booleans

&& || ! ??
Ternary

Conditional operator

? :
Bitwise

Binary operations

& | ^ ~ << >>
Type

Type checking

typeof instanceof
Other

Miscellaneous

, void delete in

➕ Arithmetic Operators

Mathematical Operations

Arithmetic operators perform mathematical calculations on numbers. JavaScript follows standard mathematical order of operations (PEMDAS).

OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division15 / 35
%Modulus17 % 52
**Exponentiation2 ** 38
++Incrementx++x + 1
--Decrementx--x - 1
🎯 Operator Precedence

JavaScript evaluates expressions in this order:

  1. Parentheses ()
  2. Exponentiation **
  3. Multiplication/Division/Modulus * / %
  4. Addition/Subtraction + -
💡 String Concatenation

The + operator also concatenates strings. When one operand is a string, JavaScript converts the other to a string and concatenates them.

Try Arithmetic Operations:
JavaScript Editor

🔄 Assignment Operators

Assigning Values

Assignment operators assign values to variables. Compound assignment operators perform an operation and assignment in one step.

OperatorExampleEquivalent ToDescription
=x = 5x = 5Simple assignment
+=x += 3x = x + 3Add and assign
-=x -= 2x = x - 2Subtract and assign
*=x *= 4x = x * 4Multiply and assign
/=x /= 2x = x / 2Divide and assign
%=x %= 3x = x % 3Modulus and assign
**=x **= 2x = x ** 2Exponentiate and assign
⚠️ Chained Assignment

Multiple assignments like a = b = c = 5 are possible but can be confusing. The assignment is processed from right to left.

🎯 Destructuring Assignment (ES6)

Extract values from arrays or properties from objects into distinct variables.

const [a, b] = [1, 2]; const {x, y} = {x: 10, y: 20};
JavaScript Editor

⚖️ Comparison Operators

Comparing Values

Comparison operators compare two values and return a boolean (true or false). They're essential for conditional statements and loops.

OperatorNameExampleDescription
==Equal to5 == '5'True (type coercion)
===Strict equal5 === '5'False (different types)
!=Not equal5 != '6'True
!==Strict not equal5 !== '5'True
>Greater than10 > 5True
<Less than10 < 5False
>=Greater or equal10 >= 10True
<=Less or equal10 <= 5False
🚫 Avoid Loose Equality

Always use === and !== instead of == and !=. Strict comparison prevents unexpected type coercion bugs.

🔍 Type Coercion Quirks

Loose equality can produce surprising results:

  • 0 == false → true
  • '' == false → true
  • null == undefined → true
  • '0' == false → true
JavaScript Editor

🔗 Logical Operators

Boolean Logic

Logical operators combine boolean values and return a boolean result. They're fundamental for conditional logic and flow control.

OperatorNameExampleDescription
&&Logical ANDa && bTrue if both are true
||Logical ORa || bTrue if at least one is true
!Logical NOT!aTrue if operand is false
??Nullish Coalescinga ?? bReturns b if a is null/undefined
⚡ Short-circuit Evaluation

JavaScript stops evaluating logical expressions as soon as the result is determined. This is used for conditional execution and default values.

🎯 Truthy and Falsy Values

In logical operations, values are converted to booleans:

Falsy:
  • false
  • 0
  • ""
  • null
  • undefined
  • NaN
Truthy:
  • Everything else!
  • "hello"
  • 42
  • []
  • true
JavaScript Editor

❓ Ternary Operator

Conditional Operator

The ternary operator is a compact way to write simple if-else statements. It takes three operands and returns one of two values based on a condition.

SyntaxEquivalent If-ElseDescription
condition ? expr1 : expr2if (condition) { expr1 } else { expr2 }Returns expr1 if true, expr2 if false
Best Practices:
  • Use for simple, single-line conditions
  • Avoid complex nested ternary operators
  • Keep expressions simple and readable
  • Use parentheses for complex conditions
💡 When to Use Ternary

Perfect for: variable assignments, return statements, and simple conditional rendering. Use if-else for complex logic with multiple statements.

🚫 Avoid This (Too Complex):
const result = a > b ? (c < d ? e : f) : (g > h ? i : j);

Instead, use if-else for complex nested conditions.

JavaScript Editor

🔢 Bitwise Operators

Binary Operations

Bitwise operators work on 32-bit binary representations of numbers. They're less common in web development but useful for performance optimization and low-level programming.

OperatorNameExampleDescription
&AND5 & 31 (0101 & 0011 = 0001)
|OR5 | 37 (0101 | 0011 = 0111)
^XOR5 ^ 36 (0101 ^ 0011 = 0110)
~NOT~5-6 (~0101 = 1010)
<<Left shift5 << 110 (0101 << 1 = 1010)
>>Right shift5 >> 12 (0101 >> 1 = 0010)
>>>Zero-fill right shift5 >>> 12 (0101 >>> 1 = 0010)
🎯 Practical Uses

Bitwise operators are great for: flags/permissions, color manipulation, cryptography, and performance-critical calculations.

💡 Common Bitwise Tricks
  • n & 1 - Check if number is odd
  • n & (n-1) === 0 - Check if power of 2
  • n << 1 - Multiply by 2
  • n >> 1 - Divide by 2 (floor)
  • a ^ b ^ b - Swap numbers
JavaScript Editor

🔍 Type Operators

Type Checking

Type operators help determine the type of values, which is crucial in JavaScript's dynamically typed environment.

OperatorNameExampleDescription
typeofType oftypeof 42"number"
instanceofInstance ofarr instanceof Arraytrue
typeof Return Values:
  • "undefined" - for undefined values
  • "boolean" - for boolean values
  • "number" - for numbers (including NaN)
  • "string" - for strings
  • "symbol" - for symbols (ES6)
  • "function" - for functions
  • "object" - for objects, arrays, and null
⚠️ typeof Quirks
  • typeof null returns "object" (historical bug)
  • typeof NaN returns "number"
  • typeof [] returns "object"
  • Use Array.isArray() for array detection
🎯 instanceof Usage

Checks if an object is an instance of a constructor function. Works with built-in types (Array, Date, RegExp) and custom constructors.

🔧 Better Type Checking
function getType(value) { if (value === null) return "null"; if (Array.isArray(value)) return "array"; return typeof value; }
JavaScript Editor

🎮 Practice: Build Utility Functions

Apply all the operators you've learned by implementing these utility functions. Each function uses different types of operators to solve common programming problems.

JavaScript Editor
💪 Additional Challenges:
  • Create a function that validates email format
  • Build a simple calculator with all operations
  • Create a password strength checker
  • Implement a temperature converter
  • Create a function that formats currency
  • Build a random number generator with range

📚 Operator Summary

Key Takeaways:
  • Use appropriate operators for different tasks
  • Always prefer strict equality (===/!==)
  • Understand operator precedence for complex expressions
  • Use ternary operator for simple conditional assignments
Best Practices:
  • Use parentheses to clarify complex expressions
  • Avoid nested ternary operators for readability
  • Use logical operators for default values and conditional execution
  • Understand truthy/falsy values in logical operations

Now that you understand operators, you're ready to learn how to control the flow of your programs!

Next: Control Flow →

📋 Operators Quick Reference

Arithmetic:
+ - * / % ** ++ --
Assignment:
= += -= *= /= %= **=
Comparison:
== === != !== > < >= <=
Logical:
&& || ! ??
Bitwise:
& | ^ ~ << >> >>>
Other:
? : typeof instanceof