Operators are symbols used to perform operations on values and variables. They are commonly used for calculations, comparisons, and assignments in JavaScript.
Note: JavaScript also supports comparison, logical, and ternary operators, which are covered in later sections.
Arithmetic operators are used to perform mathematical operations.
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2The assignment operator = is used to assign a value to a variable.
let count = 10; // 10Compound assignment operators combine an arithmetic operation with assignment. They provide a shorter and cleaner way to update variable values.
let score = 10;
score += 5; // same as score = score + 5
score -= 2; // same as score = score - 2
score *= 3; // same as score = score * 3
score /= 2; // same as score = score / 2
console.log(score); // 19.5Note: Compound assignment operators improve code readability and help reduce repetitive code.
typeof is used to check the data type of a value or variable. It is mainly used for type checking and debugging while writing JavaScript code.
let name = "Jagan";
let age = 25;
console.log(typeof name); // string
console.log(typeof age); // numberNote:
typeofalways returns a string representing the data type (for example:"string","number","boolean").