JavaScript Operators: The Basics You Need to Know

In programming , we need to do some operation with data, calculation and store the result. example : for mathematical calculation we use arithmetic operators like +, -, *, /, %. It is used for logic building and calculation. In JavaScript, The operators is just function that takes parameters which called operand, there are types of operators like Binary, Unary or Tertiary.
Every operators return different type of data.
Arithmetic operator : It is just mathematic operator. It is used for basics math operations like addition, subtraction, multiplication, division and remainder.
Comparison operator : It is used for comparing data. It is used for making decision and it return Boolean. like > , < , >=, <=,!==
Assignment operator : It is used for assigning data like =, +=, -=, *=,++, --
Logical Operator : This is used for combining logics in conditionals using || , &&, !
Truth Table
Examples for all operators
Arithmetic operators
// addition
let a =2, b = 3;
console.log(a+b) // 5
console.log(a-b)// -1
console.log(a*b)// 6
console.log(a / b) // 0.666
console.log(a % b) // 2
Comparison operator
// comparison like >, >=, <, <=, ==, ===, !==, !=
let a=1, b=2;
// greater than
console.log(a > b) // false
// less than
console.log(a < b) // true
// greater than equal
console.log(a >= b) // false
// less than equal
console.log(a <= b) // true
// double equal
console.log(a == b) // false
// triple equal
console.log(a === b) // false
// not equal to
console.log(a !== b) // true
Logical operator
// logical operator
// want to make decison like after 4 baje wakeup
// between 12 and 4 sleeping
let age = 20
if(age >=18)
console.log("You can give vote")
else
console.log("You are not allowed to give vote")
Assignment operator
// assignment
let a = 10;
console.log(a +=20) // 30
console.log(a -=20) //10
console.log(a *=20) //200
console.log(a++) //200
console.log(++a)//202
console.log(a--) // 202
console.log(--a) //200
Complex Operator Precedence
Operator precedence determines the order in which operators in an expression are calculated. This is similar as PEMDAS / BODMAs.
It also depends upon associativity of operator . It is of two ways : left to right and right to left.
// operator
let a=b=10 ;
// here value of a =10 and b=10;
// first b =10 and
// then a = b which is 10
// this is associativity which is right to left
let result = 2 + 4 ** 3 //
// first exponential done which is ** 3
// addition done
// this is operator precedence ** has high precedence than +
// Note simple for no confusion just used parens () for grouping
let sum = 2 + 5 + (3 * 2)
Difference between double and triple equal
double equality is loosely equality so it doesnot check type it only check value like 2 =="2" JS do coherce on data due to this many bug arise so we try to use === instead of == . === is strict equality so it check both type and value so it is reliable and most used instead of ==.
Summary
operator is used for logic building and solving calculations and return the results. There are many operators for the different purpose and it return different type of data which make easier to process and used the data for logic building.




