JavaScript Crash Course for Beginners-4
Welcome to part-4 of the series. You can find part-3 here.
Conditionals
The first conditional which we are going to check is the if statement. The statement within the curly brackets will execute only if the condition is true.
We compare two things in JavaScript with the == operator.
const x = '10';if(x == 10){
console.log('x is 10');
}
So, here the console.log statement will be printed. But there is a problem in the above statement because x is string 10 and we are comparing it with numeric 10. Both are becoming equal because JavaScript changes the type of string to number before comparing.
We can solve this by using === operator, which does strict comparison and doesn’t changes the type of operands.
The below statement also shows a if-else statement, in which the else part will run when the if part is false.
const y = '20';
if(y === 20){
console.log('y is 20');
} else {
console.log('y is string 20');
}
We can see both above statements in action in the below jsbin.
You can read rest of the article from my blog post. The link for the same is below.
This completes our JavaScript Crash course.
You can find the jsbins used in this part below.
https://jsbin.com/wohemad/3/edit?js,console
https://jsbin.com/dexovuy/3/edit?js,console
https://jsbin.com/tuluses/2/edit?js,console
https://jsbin.com/dowuqur/edit?js,console