JavaScriptBasics
JavaScript Operators
Learn JavaScript operators including arithmetic, assignment, comparison, logical, and unary operators, with clear examples and real-world use cases for beginners.
What are Operators in JavaScript?Link to this section
Operators are special symbols used to perform operations on values and variables. They allow JavaScript programs to calculate values, compare data, assign values, and make decisions.
Without operators, JavaScript would not be able to perform logic, calculations, or conditional checks.
Types of JavaScript Operators Link to this section
JavaScript provides several types of operators, each designed for a specific purpose:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Unary Operators
Arithmetic Operators :Link to this section
Arithmetic operators are used to perform mathematical calculations.
Common Arithmetic Operators
- `+` Addition - `-` Subtraction - `*` Multiplication - `/` Division - `%` Modulus (remainder)note
The % operator is commonly used to check even or odd numbers.
Assignment Operators :Link to this section
Assignment operators are used to assign values to variables.
Common Assignment Operators
- `=` Assign - `+=` Add and assign - `-=` Subtract and assign - `*=` Multiply and assigntip
Compound assignment operators make code shorter and easier to read.
Comparison OperatorsLink to this section
Comparison operators are used to compare two values and return a boolean result (true or false).
Common Comparison Operators
- `==` Equal (value only) - `===` Strict equal (value + type) - `!=` Not equal - `!==` Strict not equal - `>` Greater than - `<` Less thanwarning
Always prefer === over == to avoid unexpected type conversion bugs.
Logical OperatorsLink to this section
Logical operators are used to combine multiple conditions.
Logical Operators
- `&&` AND - `||` OR - `!` NOTtip
Logical operators are heavily used in conditions, authentication, and validation logic.
Unary OperatorsLink to this section
Unary operators operate on a single operand.
Common Unary Operators
- `++` Increment - `--` Decrement - `typeof` Type checkingnote
Increment and decrement operators are often used in loops.
Operator PrecedenceLink to this section
Operator precedence determines which operation is performed first in an expression.
Example explanation: Multiplication and division are evaluated before addition and subtraction unless parentheses are used.
Example explanation: Multiplication and division are evaluated before addition and subtraction unless parentheses are used.
tip
Use parentheses () to make expressions clear and avoid confusion.
Check Your Understanding
Question 1 of 5Which operator is used to check both value and type?
Arithmetic Practice
Write a program to calculate the total price after adding tax to a product....
Comparison Check
Compare a number with a string version of the same number using == and ===. Observe the difference....
Logical Conditions
Write a condition that checks whether a user is logged in and has admin access....
Increment Usage
Create a counter variable and increment it three times using the ++ operator....