'Difference between (age==18) and (age===18) in JavaScript? [duplicate]

I was doing this program and not getting output but if I entered age 18 after typecasting the age in the second block of code I got the output why is it so?

And if I use only two "==" sign then also I got the output but in case of "===" I didn't get the output.

  • age == 18 Got the output
  • Number(age)===18 Got the output
  • age === 18 Didn't got the output

Both the codes are given below.

With "==="

var age=prompt("Enter your age");
if(age<18){
  alert("Sorry, You are too young to drive this car Powering off");
}else if(age>18){
  alert("Powering On Enjoy the ride!");
}else if(age===18){
  alert("Congratulation on your first riding Enjoy the ride!");
}

With typecasting "Number(age)===18"

var age = prompt("What is your age?");
if (Number(age) < 18) {
  alert("Sorry, you are too young to drive this car. Powering off");
} else if (Number(age) > 18) {
  alert("Powering On. Enjoy the ride!");
} else if (Number(age) === 18) {
  alert("Congratulations on your first year of driving. Enjoy the ride!");
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source