JavaScript Operators Guide
JavaScript provides a variety of operators that allow you to perform operations on variables and values. This guide covers the main types of operators in JavaScript.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder)++
: Increment--
: Decrement**
: Exponentiation
Example Code
// Arithmetic Operators (+,-,*,/,%,++,--,**)
let rem = 47 % 3; // Remainder of 47 divided by 3
let num = 4; //
console.log(num++); // Outputs: 4 (num is incremented after this line)
console.log(num); // Outputs: 5 (num is now 5 after increment)
console.log(5 ** 4); // Outputs: 625 (5 raised to the power of 4) //
console.log(rem); // Outputs: 2 (the remainder)
Explanation
rem
calculates the remainder of 47 divided by 3, which is 2.num++
incrementsnum
by 1 but returns the original value (4) before the increment.5 ** 4
calculates 5 raised to the power of 4, resulting in 625.
2. Assignment Operators
Assignment operators are used to assign values to variables.
=
: Assigns a value+=
: Adds and assigns-=
: Subtracts and assigns*=
: Multiplies and assigns/=
: Divides and assigns
Example Code
// Assignment Operators (=,+=,-=,*=,/=)
let bi = 32;
bi -= 16; // bi = bi - 16, bi is now 16
bi *= 2; // bi = bi * 2, bi is now 32
bi /= 2; // bi = bi / 2, bi is now 16
let a = b = c = 66; // Assigns 66 to a, b, and c //
console.log([a, b, c]); // Outputs: [66, 66, 66]
Explanation
bi
is initially set to 32. The subsequent operations modify its value through subtraction, multiplication, and division.- The line
let a = b = c = 66;
assigns the value 66 to variablesa
,b
, andc
simultaneously.
3. Comparison Operators
Comparison operators are used to compare two values and return a boolean result.
==
: Equal to===
: Strict equal to (checks value and type)!=
: Not equal to!==
: Strict not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal to
Example Code
// Comparison Operators (==,<,>,<=,>=,===,!==,!=) //
console.log(2 == 2); // true //
console.log(2 == 4); // false //
console.log(2 > 2); // false //
console.log(2 >= 8); // false //
console.log(2 != 2); // false //
console.log('2' === 2); // false (strict comparison) //
console.log(null >= 1); // false //
console.log(undefined >= 1); // false
Explanation
- Comparison operators evaluate the relationship between two values, returning
true
orfalse
. - For example,
2 == 2
returnstrue
, while'2' === 2
returnsfalse
due to type difference.
4. Logical Operators
Logical operators are used to combine multiple boolean expressions.
&&
: Logical AND||
: Logical OR!
: Logical NOT??
: Nullish coalescing operator
Example Code
// Logical Operators (&&,||,!,??) //
console.log(2 == 2 && 1 == 1); // true //
console.log(2 == 4 || 1 == 0); // false //
console.log(!(2 < 7)); // false
let score = 9;
let newScore = score ?? 0; // newScore is 9 since score is not null or undefined
console.log(newScore); // Outputs: 9
Explanation
- Logical operators evaluate boolean expressions and return a boolean result.
- The nullish coalescing operator
??
returns the right-hand operand when the left-hand operand isnull
orundefined
.
Conclusion
Understanding operators is crucial for effective programming in JavaScript. This guide provides a foundational overview of the various operators available, along with practical examples to illustrate their usage.
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch