JavaScript Operators: The Basics
CSE Under grad. || Trying to be a better developer each day || Consistency is the key
introduction
lets take an math example - 6 + 7 = 13 here 6 & 7 are operands and ' + ' is an operator , 13 is the result of the operation .
similarly - JavaScript operators are symbols or keywords used to perform operations on values and variables.
now lets talk about various kinds of operators :-
Arithmetic operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 1+2=3 |
| - | subtraction | 2-1=1 |
| * | multiplication | 1*2=2 |
| / | division | 4/2=2 |
| % | Modulus | 3%2=1 |
Comparison operators :
Comparison operators are used to compare two values. They always return true or false.
| operator | means |
|---|---|
| == | Equal to |
| === | Strictly equals to |
| != | not equals |
| > | grater than |
| < | less than |
The diffrence bw "==" and "===" is that - first one is loose equality, it checks only value euality and second one is strict , its checks both value and type , like comparing a sweet and sour potato chip - "==" returns true as both are potato chip , but "===" returns false as one is sour and one is sweet , similarly
let a=5
let b="5"
// If we compare a==b , it will return true , if we do a===b , then it will return false
Logical operators
| operator | means |
|---|---|
| && | AND operator , returns true if and only if both conditions are true |
| ! | Not operator , it reverse an bool value |
Assignment operators
| operator | meaning |
|---|---|
| = | assign a value , like a = 5 ,,, I am assigning 5 to a variable 'a' |
| += | add and assign an value , like : a+=5 , means add 5 to the previous value of a and then reassign it to the new value its like - a = a+5 |
| -= | similar like above but first subtracts and then assign it |