Skip to content

Latest commit

 

History

History
58 lines (38 loc) · 1.58 KB

File metadata and controls

58 lines (38 loc) · 1.58 KB

05 Operators

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.

05.1 Arithmetic Operators

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); // 2

05.2 Assignment Operator

The assignment operator = is used to assign a value to a variable.

let count = 10; // 10

05.3 Compound Assignment

Compound 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.5

Note: Compound assignment operators improve code readability and help reduce repetitive code.

05.4 typeof Operator

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); // number

Note: typeof always returns a string representing the data type (for example: "string", "number", "boolean").