Simple else if not being executed

New to Javascript but not programming in general and my else if won’t execute when I type in 31. Anyone know why this wouldn’t work?

var age = prompt(“Enter age”);

if(age >= 21) {
alert(“Good”);
}

else if (age === 31) {
console.log(“Perfect”);
}

You are using the strict equality operator, so since age is a string (from the prompt) and 31 is a number, they are not strictly equal. If you use == instead, the else if condition will evaluate to true and “Perfect” will be displayed to the console.

Actually, the main reason the console.log does not execute is that if you enter 31 for the prompt, your first if statement evaluates to true, because age gets coerced into a number and since 31 is greater than or equal to 21,

I swore the guy in the tutorial I watched said use “====” all the time because it’s safer. Anyhow, thanks.

It is only “safer” if you want to only compare numbers with other numbers.

It’s still not working for me. Nothing appears in the console when == is used.

See my 2nd comment of my first reply to you.

Got it to work doing this. Can’t say I’m a fan of how Javascript handles this.

Edit: Didn’t know there was a typeof method. Good to know. I literally picked up this language for the first time like an hour ago.

var age = Number(prompt("Enter age"));

if(age <= 21) {
	alert("Good");
}

else if (age == 31) {
	console.log("Perfect");
}