JavaScript if / else statements
JavaScript Statements Reference Manual
Examples
If the current time (hours) is less than 20:00, the output of "Good day" in id = "demo" element:
if (time <20) {
document.getElementById ( "demo") innerHTML = "Good day".;
}
Output:
try it"
The bottom of the article contains more examples.
Definition and Usage
if / else statements in a specified condition is true, execute the code block. If the condition is false, another block of code is executed.
if / else statement is part of JavaScript conditional statement, conditional statement based on different criteria to perform different actions.
In JavaScript, we can use the following conditional statement:
- if statement - only if a specified condition is true, use this statement to execute code.
- else if statementif the condition statement is false, then the code block execution
- else if statement - detected a new condition if the first condition is false
- switch statement - select one of a plurality of code block execution
Browser Support
Statements | |||||
---|---|---|---|---|---|
if / else | Yes | Yes | Yes | Yes | Yes |
grammar
if statement specifies a block of code if the condition is true executed:
If the condition is true execute the code block
}
else statement specifies the code block executed if the condition is false:
If the condition is true execute the code block
} Else {
If the condition is false to execute the code block
}
else if statement specifies a new condition is false the first condition:
If condition1 is true to execute the code block
}Else if (condition2) {
If condition1 is false and condition2 execute the code block is true
} Else {
If condition1 is false and false condition2 to execute the code block
}
Parameter Value
parameter | description |
---|---|
condition | have to. Expressions for conditional: true or false |
technical details
JavaScript version: | 1.0 |
---|
More examples
Examples
If the time is less than 20:00, to generate a "Good day" greeting. Otherwise, output "Good evening":
if (time <20) {
greeting = "Good day";
} Else {
greeting = "Good evening";
}
The output is a greeting:
try it"
Examples
If the time is less than 10:00, the output of "Good morning" greeting, if time is less than 20:00, the output of "Good day" greeting. Otherwise, output "Good evening":
if (time <10) {
greeting = "Good morning";
} Else if (time <20) {
greeting = "Good day";
} Else {
greeting = "Good evening";
}
Greeting output is:
try it"
Examples
Modify the document in the first id equal "myDIV" the <div> element font number:
if (x.id == "myDIV") {
x.style.fontSize = "30px";
}
try it"
Examples
Modify the src attribute of <img> element in the user clicks on the image:
<Script>
function changeImage () {
var image = document.getElementById ( "myImage");
if (image.src.match ( "bulbon")) {
image.src = "pic_bulboff.gif";
} Else {
image.src = "pic_bulbon.gif";
}
}
</ Script>
try it"
Examples
Validate data inputs:
// Get id = value "numb" input box
x = document.getElementById ( "numb") value.;
// If not for a digital x or x is less than 1 or greater than 10 outputs "Please enter a legal value"
// If the value of x is between 1 and 10, the output "to enter the correct"
if (isNaN (x) || x <1 || x> 10) {
text = "Please enter a legal value";
} Else {
text = "Enter the correct";
}
document.getElementById ( "demo") innerHTML = text.;
try it"
Related Pages
JavaScript Tutorials: JavaScript the If ... Else statement
JavaScript Tutorials: JavaScript statements of Switch
JavaScript Statements Reference Manual