Javascript not running “else” or “===” but works for “if” and “else if”
up vote
-2
down vote
favorite
Hello I'm new to javascript and trying to run the following
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else (age > 19) {
document.write("you can already drink");
}
I can't seem to get anything to show up but if I change my code to remove the else statement and change the ===
to just ==
my code runs. This is how it looks like when it runs after I get rid of the else statement and ===
operator.
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age == 19) {
document.write("you can finally drink");
}
I'm trying to get the first block of code to run but can't. The page is empty and no prompt shows up. Someone please help.
javascript
add a comment |
up vote
-2
down vote
favorite
Hello I'm new to javascript and trying to run the following
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else (age > 19) {
document.write("you can already drink");
}
I can't seem to get anything to show up but if I change my code to remove the else statement and change the ===
to just ==
my code runs. This is how it looks like when it runs after I get rid of the else statement and ===
operator.
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age == 19) {
document.write("you can finally drink");
}
I'm trying to get the first block of code to run but can't. The page is empty and no prompt shows up. Someone please help.
javascript
3
else (age > 19)
doesnt make much sense, else is when all if and else if are false, you dont need any condition.
– Chris Li
Sep 27 at 1:22
1
There is no condition in an else branch, it's simply ignored. See The if Statement, the (simplified) form is:if (expression) { statement } else { statement }
.
– RobG
Sep 27 at 1:25
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Hello I'm new to javascript and trying to run the following
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else (age > 19) {
document.write("you can already drink");
}
I can't seem to get anything to show up but if I change my code to remove the else statement and change the ===
to just ==
my code runs. This is how it looks like when it runs after I get rid of the else statement and ===
operator.
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age == 19) {
document.write("you can finally drink");
}
I'm trying to get the first block of code to run but can't. The page is empty and no prompt shows up. Someone please help.
javascript
Hello I'm new to javascript and trying to run the following
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else (age > 19) {
document.write("you can already drink");
}
I can't seem to get anything to show up but if I change my code to remove the else statement and change the ===
to just ==
my code runs. This is how it looks like when it runs after I get rid of the else statement and ===
operator.
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age == 19) {
document.write("you can finally drink");
}
I'm trying to get the first block of code to run but can't. The page is empty and no prompt shows up. Someone please help.
javascript
javascript
edited Sep 27 at 7:17
Vladimir Vagaytsev
2,14492327
2,14492327
asked Sep 27 at 1:16
monkeycigarette
9
9
3
else (age > 19)
doesnt make much sense, else is when all if and else if are false, you dont need any condition.
– Chris Li
Sep 27 at 1:22
1
There is no condition in an else branch, it's simply ignored. See The if Statement, the (simplified) form is:if (expression) { statement } else { statement }
.
– RobG
Sep 27 at 1:25
add a comment |
3
else (age > 19)
doesnt make much sense, else is when all if and else if are false, you dont need any condition.
– Chris Li
Sep 27 at 1:22
1
There is no condition in an else branch, it's simply ignored. See The if Statement, the (simplified) form is:if (expression) { statement } else { statement }
.
– RobG
Sep 27 at 1:25
3
3
else (age > 19)
doesnt make much sense, else is when all if and else if are false, you dont need any condition.– Chris Li
Sep 27 at 1:22
else (age > 19)
doesnt make much sense, else is when all if and else if are false, you dont need any condition.– Chris Li
Sep 27 at 1:22
1
1
There is no condition in an else branch, it's simply ignored. See The if Statement, the (simplified) form is:
if (expression) { statement } else { statement }
.– RobG
Sep 27 at 1:25
There is no condition in an else branch, it's simply ignored. See The if Statement, the (simplified) form is:
if (expression) { statement } else { statement }
.– RobG
Sep 27 at 1:25
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
There are a few mistakes in your code:
1) prompt
method return string
no number
, so :
Use ==
instead of ===
:
else if (age == 19) {
PS: (19 == '19'); is true
but (19 === '19'); is false
OR convert age
to number :
else if (Number(age) === 19) {
2) You should not use condition for else
, so you must change else
like this:
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
add a comment |
up vote
0
down vote
Your problem is the last else statement. You are putting a condition after the else, which does not work. You only add the condition after another if. You can fix this by removing the condition like so:
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
add a comment |
up vote
0
down vote
You seem to be having some logical error in your last else
statement. If none of the first 2 conditions are true, then logically speaking, the last condition must be true. else
does not hold any conditions. It's simply just else in terms of the previous condition(s). Else you would have to use else if()
again, but as mentioned earlier, in case neither of the conditions are true, then the last condition must be the case regardless in this example. Therefore, just else makes the most sense.
Example:
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
Another thing in terms of your operators. ==
means equal to, and can be used to compare different types. I.e. you can compare a number type to a string type, and if they both hold the same value, the statement will be true. However, ===
is a strict equals, and means that the things being compared must be the same value AND the same type.
Example:
var x = 5; //number
var y = '5'; //string
if(x == y) {
//true, because they are equal, both has the value 5
}
if(x === y) {
//false, because even though they are equal in value, they are not the same type
}
var a = 8; //number
var b = 8; //number
if(a === b) {
//true, because they are both equal, and the same type
}
So, to clearify,
==
checks if the values are equal, no matter the type.
===
checks if the values are equal AND are of the same type.
More simple documentation on operators here.
add a comment |
up vote
0
down vote
Firstly you need to understant differnce between "==" and "===" . From this you can conclude
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
And secondly "else" with a condition is always written as "else if" ...else is block which is runs when none of its above conditions is true i.e none is true ..read from here for more information
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
There are a few mistakes in your code:
1) prompt
method return string
no number
, so :
Use ==
instead of ===
:
else if (age == 19) {
PS: (19 == '19'); is true
but (19 === '19'); is false
OR convert age
to number :
else if (Number(age) === 19) {
2) You should not use condition for else
, so you must change else
like this:
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
add a comment |
up vote
1
down vote
There are a few mistakes in your code:
1) prompt
method return string
no number
, so :
Use ==
instead of ===
:
else if (age == 19) {
PS: (19 == '19'); is true
but (19 === '19'); is false
OR convert age
to number :
else if (Number(age) === 19) {
2) You should not use condition for else
, so you must change else
like this:
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
add a comment |
up vote
1
down vote
up vote
1
down vote
There are a few mistakes in your code:
1) prompt
method return string
no number
, so :
Use ==
instead of ===
:
else if (age == 19) {
PS: (19 == '19'); is true
but (19 === '19'); is false
OR convert age
to number :
else if (Number(age) === 19) {
2) You should not use condition for else
, so you must change else
like this:
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
There are a few mistakes in your code:
1) prompt
method return string
no number
, so :
Use ==
instead of ===
:
else if (age == 19) {
PS: (19 == '19'); is true
but (19 === '19'); is false
OR convert age
to number :
else if (Number(age) === 19) {
2) You should not use condition for else
, so you must change else
like this:
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
var age = prompt("What is your age");
if (age < 19) { document.write("You can't drink"); }
else if (Number(age) === 19) { document.write("you can finally drink"); }
else { document.write("you can already drink"); }
edited Nov 10 at 21:06
halfer
14.2k757106
14.2k757106
answered Sep 27 at 7:47
Ehsan
9,49531129
9,49531129
add a comment |
add a comment |
up vote
0
down vote
Your problem is the last else statement. You are putting a condition after the else, which does not work. You only add the condition after another if. You can fix this by removing the condition like so:
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
add a comment |
up vote
0
down vote
Your problem is the last else statement. You are putting a condition after the else, which does not work. You only add the condition after another if. You can fix this by removing the condition like so:
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Your problem is the last else statement. You are putting a condition after the else, which does not work. You only add the condition after another if. You can fix this by removing the condition like so:
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
Your problem is the last else statement. You are putting a condition after the else, which does not work. You only add the condition after another if. You can fix this by removing the condition like so:
var age = prompt("What is your age");
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
answered Sep 27 at 1:24
2pacalypse
522
522
add a comment |
add a comment |
up vote
0
down vote
You seem to be having some logical error in your last else
statement. If none of the first 2 conditions are true, then logically speaking, the last condition must be true. else
does not hold any conditions. It's simply just else in terms of the previous condition(s). Else you would have to use else if()
again, but as mentioned earlier, in case neither of the conditions are true, then the last condition must be the case regardless in this example. Therefore, just else makes the most sense.
Example:
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
Another thing in terms of your operators. ==
means equal to, and can be used to compare different types. I.e. you can compare a number type to a string type, and if they both hold the same value, the statement will be true. However, ===
is a strict equals, and means that the things being compared must be the same value AND the same type.
Example:
var x = 5; //number
var y = '5'; //string
if(x == y) {
//true, because they are equal, both has the value 5
}
if(x === y) {
//false, because even though they are equal in value, they are not the same type
}
var a = 8; //number
var b = 8; //number
if(a === b) {
//true, because they are both equal, and the same type
}
So, to clearify,
==
checks if the values are equal, no matter the type.
===
checks if the values are equal AND are of the same type.
More simple documentation on operators here.
add a comment |
up vote
0
down vote
You seem to be having some logical error in your last else
statement. If none of the first 2 conditions are true, then logically speaking, the last condition must be true. else
does not hold any conditions. It's simply just else in terms of the previous condition(s). Else you would have to use else if()
again, but as mentioned earlier, in case neither of the conditions are true, then the last condition must be the case regardless in this example. Therefore, just else makes the most sense.
Example:
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
Another thing in terms of your operators. ==
means equal to, and can be used to compare different types. I.e. you can compare a number type to a string type, and if they both hold the same value, the statement will be true. However, ===
is a strict equals, and means that the things being compared must be the same value AND the same type.
Example:
var x = 5; //number
var y = '5'; //string
if(x == y) {
//true, because they are equal, both has the value 5
}
if(x === y) {
//false, because even though they are equal in value, they are not the same type
}
var a = 8; //number
var b = 8; //number
if(a === b) {
//true, because they are both equal, and the same type
}
So, to clearify,
==
checks if the values are equal, no matter the type.
===
checks if the values are equal AND are of the same type.
More simple documentation on operators here.
add a comment |
up vote
0
down vote
up vote
0
down vote
You seem to be having some logical error in your last else
statement. If none of the first 2 conditions are true, then logically speaking, the last condition must be true. else
does not hold any conditions. It's simply just else in terms of the previous condition(s). Else you would have to use else if()
again, but as mentioned earlier, in case neither of the conditions are true, then the last condition must be the case regardless in this example. Therefore, just else makes the most sense.
Example:
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
Another thing in terms of your operators. ==
means equal to, and can be used to compare different types. I.e. you can compare a number type to a string type, and if they both hold the same value, the statement will be true. However, ===
is a strict equals, and means that the things being compared must be the same value AND the same type.
Example:
var x = 5; //number
var y = '5'; //string
if(x == y) {
//true, because they are equal, both has the value 5
}
if(x === y) {
//false, because even though they are equal in value, they are not the same type
}
var a = 8; //number
var b = 8; //number
if(a === b) {
//true, because they are both equal, and the same type
}
So, to clearify,
==
checks if the values are equal, no matter the type.
===
checks if the values are equal AND are of the same type.
More simple documentation on operators here.
You seem to be having some logical error in your last else
statement. If none of the first 2 conditions are true, then logically speaking, the last condition must be true. else
does not hold any conditions. It's simply just else in terms of the previous condition(s). Else you would have to use else if()
again, but as mentioned earlier, in case neither of the conditions are true, then the last condition must be the case regardless in this example. Therefore, just else makes the most sense.
Example:
if (age < 19) {
document.write("You can't drink");
}
else if (age === 19) {
document.write("you can finally drink");
}
else {
document.write("you can already drink");
}
Another thing in terms of your operators. ==
means equal to, and can be used to compare different types. I.e. you can compare a number type to a string type, and if they both hold the same value, the statement will be true. However, ===
is a strict equals, and means that the things being compared must be the same value AND the same type.
Example:
var x = 5; //number
var y = '5'; //string
if(x == y) {
//true, because they are equal, both has the value 5
}
if(x === y) {
//false, because even though they are equal in value, they are not the same type
}
var a = 8; //number
var b = 8; //number
if(a === b) {
//true, because they are both equal, and the same type
}
So, to clearify,
==
checks if the values are equal, no matter the type.
===
checks if the values are equal AND are of the same type.
More simple documentation on operators here.
edited Sep 27 at 8:28
answered Sep 27 at 8:22
Martin
906211
906211
add a comment |
add a comment |
up vote
0
down vote
Firstly you need to understant differnce between "==" and "===" . From this you can conclude
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
And secondly "else" with a condition is always written as "else if" ...else is block which is runs when none of its above conditions is true i.e none is true ..read from here for more information
add a comment |
up vote
0
down vote
Firstly you need to understant differnce between "==" and "===" . From this you can conclude
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
And secondly "else" with a condition is always written as "else if" ...else is block which is runs when none of its above conditions is true i.e none is true ..read from here for more information
add a comment |
up vote
0
down vote
up vote
0
down vote
Firstly you need to understant differnce between "==" and "===" . From this you can conclude
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
And secondly "else" with a condition is always written as "else if" ...else is block which is runs when none of its above conditions is true i.e none is true ..read from here for more information
Firstly you need to understant differnce between "==" and "===" . From this you can conclude
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
And secondly "else" with a condition is always written as "else if" ...else is block which is runs when none of its above conditions is true i.e none is true ..read from here for more information
edited Oct 4 at 11:11
answered Sep 27 at 7:36
Priyanshi Srivastava
1009
1009
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52528067%2fjavascript-not-running-else-or-but-works-for-if-and-else-if%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
else (age > 19)
doesnt make much sense, else is when all if and else if are false, you dont need any condition.– Chris Li
Sep 27 at 1:22
1
There is no condition in an else branch, it's simply ignored. See The if Statement, the (simplified) form is:
if (expression) { statement } else { statement }
.– RobG
Sep 27 at 1:25