Hello 👋🏾 everyone,
Welcome, in this post we’ll be learning about data types in JavaScript specifically number type. In the part1 we discussed performing basic arithmetic operations on numbers and the precedence of arithmetic operators. I this post discusses how to compare numbers.
Introduction
To compare numbers in JavaScript comparison operators are used. Comparison operators compare number types and returns a Boolean value based on whether the comparison is true or false. The Boolean value returned can be stored in variables. The following paragraphs discuss comparison operators in JavaScript and how they work.
Greater Than
To check if a number is greater than another number, use the greater than operator (>
). It returns true
if and only if the lefthand side is greater than righthand side. Else it returns false
. For instance:
let a = 3;
let b = 6;
let c;
c = b > a;
console.log(c)
//-> true
// the Boolean is stored in variable c
6 > 3
//-> true
/* If the two numbers are equal
it returns `false` */
6 > 6
//-> false
3 > 6
//-> false
Greater Than or Equal to
To check if a number is greater than or equal to a number, use the greater than or equal operator. (>=
). It returns true if the lefthand side is greater or equal to the righthand side. Else it returns false
.
For instance:
7 >= 6
//-> true
6 >= 6
//-> true
3 > 6
//-> false
Lesser Than
To check if a number is lesser than another number, use the lesser than operator (<
). It returns true
if and only if the lefthand side is lesser than the righthand side. Else it returns false
. If the two numbers are equal it also returns false
For instance:
3 < 6;
//-> true
/* If the two numbers are equal
it returns `false` */
6 < 6
//-> false
6 < 3
//-> false
Lesser Than or Equal to
To check if a number is lesser than or equal to a number, use (<=
) symbol. It returns true if the lefthand side is lesser than or equal to the righthand side. Else it returns false
. For instance:
// to compare 7 and 6
6 <= 7
//-> true
6 <= 6
//-> true
7 <= 6
//-> false
Equality
To check if two numbers are equal, use the equality operator (==
). For instance:
6 == 6
//-> true
6 == 3
//-> false
6 == “6”
//-> true
Notice that in example 3 above comparing the number 6 to the string 6 returns true. This is because the equality operator converts the number 6 to a string before comparing them. This occurrence is known as type conversion. This can sometimes produce bugs in our code and it’s overcomed by using the strict equality operator.
To learn more about JavaScript types: Data types in JavaScript
Strict Equality
To check if two numbers are strictly equal to one another, use the strict equality operator (===
). The strict equality operator returns true if both the value and type is true. For instance:
6 === 6
//-> true
// to compare 3 with 6
6 === 3
//-> false
// to compare 3 with 6
6 === “6”
//-> false
NB: the strict equality operator does not perform type conversion.
To learn more about JavaScript types: Data types in JavaScript
Not Equal to
To check if two numbers are not equal, use (!=
) symbol. It returns true if the numbers are not equal and returns false
if the numbers are equal. For instance:
// to compare 7 and 6
6 != 7
//-> true
6 != 6
//-> false
Not Strictly Equal to
To check if two numbers are not strictly equal to one another, use (!==
) symbol. It returns true if the numbers are not equal and returns false
if the numbers are equal. For instance:
// to compare 7 and 6
6 != 7
//-> true
6 != 6
//-> false
Just like the equality operator, the not equal to operator also performs type conversion. Hence, the not equal to operator is recommended
Precedence of comparison operators.
Precedence of operators refers to the order in which operations are carried out when two or more operators appear at the same time.
- When comparison operators and arithmetic operators appear in the same statement, arithmetic operators are evaluated first based on their precedence as discussed in part 1 then the comparison operators are evaluated.
- The comparison operators all have the same precedence. When two or more comparison operators appear in a statement they are often used with the logical OR, AND or NOT logical operators which is outside the scope of this post.
That’s a wrap! Thank you for reading this post, hopefully it was useful. Kindly share your thoughts in the comment section. Click the follow button for useful posts on learning and mastering JavaScript as well as other technologies in web development!.
Ask me anything on twitter at @a__sally
Part 3 covering number methods is on the way! Stay tuned😁.