(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Comparison and Logical Operators.
JavaScript Comparison and Logical Operators
In this tutorial, you will learn about the Comparison operators and Logical operators with the help of examples.
JavaScript Comparison Operators
Comparison operators compare two values and give back a boolean value: either true
or false
. Comparison operators are used in decision making and loops.
Operator | Description | Example |
---|---|---|
== |
Equal to: true if the operands are equal |
5 == 5 |
!= |
Not equal to: true if the operands are not equal |
5 != 5 |
=== |
Strict equal to: true if the operands are equal and of the same type |
5 === '5' |
!== |
Strict not equal to: true if the operands are equal but of different type or not equal at all |
5 !== '5' |
> |
Greater than: true if the left operand is greater than the right operand |
3 > 2 |
>= |
Greater than or equal to: true if the left operand is greater than or equal to the right operand |
3 >= 3 |
< |
Less than: true if the left operand is less than the right operand |
3 < 2 |
<= |
Less than or equal to: true if the left operand is less than or equal to the right operand |
2 <= 2 |
Example 1: Equal to Operator
let a = 5, b = 2, c = 'hello';
// equal to operator
console.log(a == 5); // true
console.log(b == '2'); // true
console.log(c == 'Hello'); // false
==
evaluates to true
if the operands are equal.
Note: In JavaScript, ==
is a comparison operator, whereas =
is an assignment operator. If you mistakenly use =
instead of ==
, you might get unwanted result.
Example 2: Not Equal to Operator
let a = 3, b = 'hello';
// not equal operator
console.log(a != 2); // true
console.log(b != 'Hello'); // true
!=
evaluates to true
if the operands are not equal.
Example 3: Strict Equal to Operator
let a = 2;
// strict equal operator
console.log(a === 2); // true
console.log(a === '2'); // false
===
evaluates totrue
if the operands are equal and of the same type. Here 2 and ‘2’ are the same numbers but the data type is different. And ===
also checks for the data type while comparing.
Note: The difference between ==
and ===
is that:
==
evaluates to true
if the operands are equal, however, ===
evaluates to true
only if the operands are equal and of the same type
Example 4: Strict Not Equal to Operator
let a = 2, b = 'hello';
// strict not equal operator
console.log(a !== 2); // false
console.log(a !== '2'); // true
console.log(b !== 'Hello'); // true
!==
evaluates to true
if the operands are strictly not equal. It’s the complete opposite of strictly equal ===
.
In the above example, 2 != '2'
gives true
. It’s because their types are different even though they have the same value.
Example 5: Greater than Operator
let a = 3;
// greater than operator
console.log(a > 2); // true
>
evaluates to true
if the left operand is greater than the right operand.
Example 6: Greater than or Equal to Operator
let a = 3;
// greater than or equal operator
console.log(a >= 3); //true
>=
evaluates to true
if the left operand is greater than or equal to the right operand.
Example 7: Less than Operator
let a = 3, b = 2;
// less than operator
console.log(a < 2); // false
console.log(b < 3;) // true
<
evaluates to true
if the left operand is less than the right operand.
Example 8: Less than or Equal to Operator
let a = 2;
// less than or equal operator
console.log(a <= 3) // true
console.log(a <= 2); // true
<=
evaluates to true
if the left operand is less than or equal to the right operand.
JavaScript Logical Operators
Logical operators perform logical operations: AND, OR and NOT.
Operator | Description | Example |
---|---|---|
&& |
Logical AND: true if both the operands/boolean values are true, else evaluates to false |
true && false; // false |
|| |
Logical OR: true if either of the operands/boolean values is true . evaluates to false if both are false |
true || false; // true |
! |
Logical NOT: true if the operand is false and vice-versa. |
!true; // false |
Example 9: Logical AND Operator
let a = true, b = false;
let c = 4;
// logical AND
console.log(a && a); // true
console.log(a && b); // false
console.log((c > 2) && (c < 2)); // false
&&
evaluates to true
if both the operands are true
, else evaluates to false
.
Note: You can also use logical operators with numbers. In JavaScript, 0 is false
and all non-zero values are true
.
Example 10: Logical OR Operator
let a = true, b = false, c = 4;
// logical OR
console.log(a || b); // true
console.log(b || b); // false
console.log((c>2) || (c<2)); // true
||
evaluates to true
if either of the operands is true
. If both operands are false
, the result is false
.
Example 11: Logical NOT Operator
let a = true, b = false;
// logical NOT
console.log(!a); // false
console.log(!b); // true
!
evaluates to true
if the operand is false
and vice-versa.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.