JavaScript Crash Course for Beginners-4

Nabendu Biswas
2 min readAug 20, 2020

--

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.

if and if-else

--

--

Nabendu Biswas
Nabendu Biswas

Written by Nabendu Biswas

Architect, ReactJS & Ecosystem Expert, Youtuber, Blogger

No responses yet