Operators Explained , Examples & Common Mistakes
What are Operators?
- Operators are special symbols or keywords used to perform operations.
- They work on:
- Variables →
a + b - Values →
10 + 20
Example:
10 + 20 // + is an operator
Why Operators are Important?
- Used in almost every program or project
- Common in all programming languages like:
- Java
- Python
- PHP
- C#
Types of Operators in JavaScript (8 Types)
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Ternary Operators
- Type Operators
- Bitwise Operators
- String Operators
1. Arithmetic Operators
Used for mathematical operations
10 + 20 // Addition
10 - 5 // Subtraction
10 * 2 // Multiplication
2. Assignment Operators
Used to assign values
let a = 10; // = is assignment operator
a += 5; // add and assign
3. Comparison Operators
Used to compare values
10 > 5 // true
10 < 5 // false
10 == "10" // true (loose comparison)
Example from Code
let a = 10;
let b = 30;
let c = a + b; // 40
Function Example
function tryOperators() {
let a = 10;
let b = 30;
let c = a + b;
alert(c); // 40
}
Button Click Example
<button onclick="tryOperators()">Add Two Numbers</button>
When clicked, the function runs and shows the result.
Common Mistakes (Important)
Mistake 1: Using == instead of =
let a == 10; // wrong
let a = 10; // correct
Mistake 2: String + Number
alert(10 + "10"); // "1010" (string concatenation)
alert(10 + 10); // 20
Always check the data type.
Key Points to Remember
- Operators perform actions on values or variables
+can add numbers or join strings- Always use correct operator (
=,==,===) - Check errors in the console