Operators in javascript
We can use all the operators in Js just like any other programming language. So, let's just walk through each one by one.
//variables
let a=10;
let b=3;
//addition
console.log(a+b);
/*result : 3 or if one of each value was a string then it
will just concatenate with the number*/
//subtraction
console.log(a-b); //result : 7
// multiplication
console.log(a*b); //result : 30
//division
console.log(a/b); //result : 3.3333 'accurate result no round of xD'
//exponent
console.log(a**b);
//modulus or remainder
console.log(a%b);
//incrementation
console.log(a++); //result will be 10 but after that when you call it will just be 11
console.log(a--);
/*same with this result will be 11 because of the addition
but when you call it will be 10 again*/
Confussion between post increment decrement and pre-increment and decrements
//so let's just break it down
//declaring variable
let a=4;
a++;// 4 is now 5 but when you print this line it will show 4.
console.log(a);//now it will show 5
//same with the decrements
console.log(a--);//console will show 5
console.log(a);//now it will tell you that it is 4
//great example from stack overflow
a = 4;
b = a++; // first b will be 4, and after this a will be 5
// now a value is 5
c = ++a; // first a will be 6, then 6 will be assigned to c
/*
++i increments i and evaluates to the new value of i.
i++ evaluates to the old value of i, and increments i.
*/
Assignment Operator
// x=y equals to
//x+=y -> x=x+y
//x-=y -> x=x-y
//x*=y -> x=x*y
//x/=y -> x=x/y
//x%=y -> x=x%y
//x**=y -> x=x**y
Comparison operators
//x==y ->equals to
/*but will type change if values are string or number like 1 == '1'
should be false but it will change the type for
string '1' to 1 it will be true*/
// x===y ->strict equals to wont change the type will compare values with original types.
//x!=y -> not equals to
//x!==y --> not equal to value or not equal to type
//x>y -> greater than
//x<y -> less than
//x>=y ->greater than equal to
//x<=y -> less than equal to
//x?y --> ternary operator
let a=4;
let b=5;
console.log(a==b ? true:false); //result false just type of condition
Logical operators
let x=4;
let y=4;
console.log(x>y && x===y); //result false and true means false cause of AND operator
console.log(x>y || x===y); //result true due to OR operator
console.log(!false);//result true due to NOT operator
Β