JavaScript Comparison and Logical Operators ๐
๐๐งต
๐๐งต
Don't forget to bookmarked it for future reference...
Comparison operators
โข Compare two values and give back a boolean value (true or false).
โข Used in decision making and loops.
โข Example :
const a = 1, b = 3 ;
// equal to operator
console.log(a == 1); // true
console.log(b == '3'); // true
โข Compare two values and give back a boolean value (true or false).
โข Used in decision making and loops.
โข Example :
const a = 1, b = 3 ;
// equal to operator
console.log(a == 1); // true
console.log(b == '3'); // true
Types of Comparison operators
1. Equal to (==) : true if the operands are equal (2==2; //true).
2. Not equal to (!=) : true if the operands are not equal (2!=2; //false).
3. Strict equal to (===) : true if the operands are equal and of the same type (2==='2'; //false).
1. Equal to (==) : true if the operands are equal (2==2; //true).
2. Not equal to (!=) : true if the operands are not equal (2!=2; //false).
3. Strict equal to (===) : true if the operands are equal and of the same type (2==='2'; //false).
4. Strict not equal to (!==) : true if the operands are equal but of different type or not equal at all(2!=='2'; //true).
5. Greater than (>) : true if the left operand is greater than the right operand (5>1; //true).
5. Greater than (>) : true if the left operand is greater than the right operand (5>1; //true).
6. Greater than or equal to (>=) : true if the left operand is greater than or equal to the right operand (1>=1; //true).
7. Less than (<) : true if the left operand is less than the right operand (1<0; //false).
7. Less than (<) : true if the left operand is less than the right operand (1<0; //false).
8. Less than or equal to (<=) : true if the left operand is less than or equal to the right operand (1<=1; //true).
Note: Difference between == and === is == evaluates to true if the operands are equal & === evaluates to true only if the operands are equal and of the same type.
Note: Difference between == and === is == evaluates to true if the operands are equal & === evaluates to true only if the operands are equal and of the same type.
Note: In JavaScript, == is a comparison operator, whereas = is an assignment operator.
If you mistakenly use = instead of ==, you might get unwanted result.
If you mistakenly use = instead of ==, you might get unwanted result.
Logical Operators
โข Used to perform logical operations( AND, OR and NOT).
โข Example :
const a = true, b = false; const c = 4;
// logical AND
console.log(a && a); // true
console.log(a && b); // false
console.log((c > 2) && (c < 2)); // false
โข Used to perform logical operations( AND, OR and NOT).
โข Example :
const a = true, b = false; const c = 4;
// logical AND
console.log(a && a); // true
console.log(a && b); // false
console.log((c > 2) && (c < 2)); // false
Types of Logical Operators
1. Logical AND(&&): true if both the operands/boolean values are true, else evaluates to false (true&&false;//false).
2. Logical OR(||): true if either of the operands/boolean values is true. evaluates to false if both are false (true||false;// true).
1. Logical AND(&&): true if both the operands/boolean values are true, else evaluates to false (true&&false;//false).
2. Logical OR(||): true if either of the operands/boolean values is true. evaluates to false if both are false (true||false;// true).
3. Logical NOT (!) : true if the operand is false and vice-versa (!true; // false).
Note: You can also use logical operators with numbers. In JavaScript, 0 is false and all non-zero values are true.
End thread ๐งต
Note: You can also use logical operators with numbers. In JavaScript, 0 is false and all non-zero values are true.
End thread ๐งต
If you liked this thread:
-RT the first thread and share it with folks on Twitter.
-Follow @personalvipin for more such insightful threads.
Thanks for reading!
-RT the first thread and share it with folks on Twitter.
-Follow @personalvipin for more such insightful threads.
Thanks for reading!
Loading suggestions...