Home Courses Operators Explained , Examples & Common Mistakes

Operators Explained , Examples & Common Mistakes

What are Operators?

  1. Operators are special symbols or keywords used to perform operations.
  2. They work on:
  3. Variables → a + b
  4. Values → 10 + 20

Example:


10 + 20 // + is an operator


Why Operators are Important?

  1. Used in almost every program or project
  2. Common in all programming languages like:
  3. Java
  4. Python
  5. PHP
  6. C#


Types of Operators in JavaScript (8 Types)

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Ternary Operators
  6. Type Operators
  7. Bitwise Operators
  8. 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

  1. Operators perform actions on values or variables
  2. + can add numbers or join strings
  3. Always use correct operator (=, ==, ===)
  4. Check errors in the console


Share this lesson: