Hi there,
This is a second of our series on JavaScript syntax. If you haven't viewed the first part. Follow the link => https://titanium.hashnode.dev/introduction-to-javascript-syntax-part-1-ckmnnembo08c35is12lcc6ot8
For this tutorial, we will be going farther into the syntax. Here we will discuss Comparison operations, If and Switch Statements.
Let's go!
Comparison Operations
Comparison operations as the name implies involves the check done between two values for any of the following conditions.
==
(equal to)===
(strictly equal to)<
(less than)<=
(less than or equal to)>
(greater than)>=
(greater than or equal to)
Comparison can be between any data type. For example.
console.log("meat" == "meat") //this will give True
The different between equal to
and strictly equal to
is this below.
console.log(2 === "2") //this will give False
console.log(2 == "2") //this will give True
In the example above, the first results in False
because the triple equal sign is used to strictly compare two values. Which means it also checks the data type. If the data types are not the same, even if the numbers look the same. It will be false.
In this case the first 2
is a number data type while the second "2"
is a string data type.
However in the other example, 2 == "2"
will give True because, the double equals does not check the data type, instead it does something called Type Coercion.
This means that in the background, the ==
(double equals sign), forces both types to be the same and then compares their values. Hence, when "2"
is 'coerced' is becomes 2
and become equal to the number 2
.
Other comparison checks are pretty straightforward.
console.log(2 > 3) // false
console.log(2 >= 1) // true
console.log(2 < 5) // true
If Statements
Comparison checks and if
statements work closely together. The if
is a keyword in JavaScript used for checking whether a condition is true or not.
if
statements are used to execute code based on a particular condition or set of conditions.
The format for if
statements is:
if (condition) {
// do this
}
For instance, if we want to give users information based on their age we can write a code like this:
let userAge = 18
if(userAge >= 18) {
console.log("Welcome aboard!")
}
We can also do something else if the initial condition is not true using another keyword else
. i.e
if(condition is true){
// do something here
}else {
// do something different
}
Using our previous example,
let userAge = 18
if(userAge >= 18){
console.log("Welcome aboard!")
}else {
console.log("You're not allowed here!")
}
// welcome aboard
We could also check more conditions using the else if
keyword.
if(condition is true){
//do something
}else if (another condition is true){
// do something else
} else {
// do this if all fails
}
Continuing with the previous example,
let userAge = 18
if(userAge == 18){
console.log("You are right on age!")
}else if(userAge > 18){
console.log("You are above the age!")
}else {
console.log("You are below the age!")
}
//you are right on age!
Switch Statements
Similar to if
statements switch
statements also help us take action based on 'truthi-ness' of a condition. switch
statements are usually preferred when the number of conditions are long.
They are used in the format:
switch(expression){
case value1:
//do something
break;
case value2:
//do something
break;
case value3:
//do something
break;
default:
//do this if all fails
}
Example:
let today = new Date().getDay() // this gives the value of today between 0 - 6
switch (today) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
A Quick Recap
We looked at Comparison operators which are used to compare two values and returns true
or false
based on the comparison. These can also be used in if
statements which allows you take action in your code based on a condition's truthiness. And then there are switch
statements which can also function like if
statements.