JS Operators:
- Arithmetic Operators: These operators are used to perform basic mathematical operation on numbers.
List of arithmetic operators:
a) Addition operator (+) :
Ex:
let x=10;
let y=5
let z=x+y; //15
If a string is added with a number, it will produce concatenation.
Concatenation:Joining two or more strings together.
Ex:
let x="10";
let y=5;
let z=x+y;
The value of z will be "105".
console.log(true+1); //2
This is a boolean condition[true=1, false=0]
Hence 1+1 = 2
b) Subtraction operator (-) :
Ex:
let x=10;
let y=5
let z=x-y; //5
In case of any string value [ex "10"] it will change the string value to integer and perform the subtraction.
parseInt(): It is a function in JS which is used to convert string to an integer.
Ex:
let x = "10";
let y = 5;
let z = parseInt(x) + y;
Output is 15.
If the string contains any name, it will try to change into integer. Or else it will return as NAN [not a number].
c) Multiplication operator (*) :
Ex:
let x=7;
let y=3;
let z=x*y; //21
d) Division operator (/) : It gives the quotient.
Ex:
let x=10;
let y=2
let z=x/y //5
e) Modulus operator (%) : It gives the remainder.
Ex:
let x=10;
let y=2
let z=x%y //0
f) Exponential operator (**) :
Ex:
let x = 5;
let z = x ** 2; //25
g) Increment operator (++) :
This has 2 types: pre-increment and post-increment.
Post-increment:
Ex:
let i=10;
i=i+1; //11
It can also be written as
let i=10;
i++ ; //11
Pre-increment:
Ex:
let x = 5;
let y = ++x;
console.log (x); //6
console.log (y); //6
Pre-decrement:
Ex:
let j=10;
--j;
console.log(j); //9
console.log(--j); //8
Post-decrement:
Ex:
let x = 5;
let y = x--;
console.log(y); //5
console.log(x); //4
2) Comparison operators: These are used to compare two numbers and returns a boolean value.
List of comparison operators:
a) Equal to (==) : let 5==5 //true.
b) Equal value and equal type (===) : let "5"===5 // false.
c) Less than (<) : let 5<8 //true.
d) Less than or equal to (<=) : let 5<=5 //true.
e) Greater than (>) : let 8>5 //true.
f) Greater than or equal to (>=) : let 8>=5 //true.
Ex:
let i=5;
let j=7;
++i > --j // 6>6 which is false.
- Logical operators: Multiple conditions can be combined using logical operators. They use logic to return true or false.
List of logical operators:
a) logical and (&&) - returns true only if both conditions are true.
Ex:
console.log(5 > 3 && 10 > 2); // true
b) logical or (||) - returns true if at least one condition is true and returns false only if both conditions are false.
Ex:
console.log(5 > 3 || 1 > 2); //true
console.log(1 > 5 || 2 > 4); //false
c) logical not (!) - The condition becomes reverse like true becomes false, false becomes true.
Top comments (0)