[ JavaScript Statement Reference Manual](#)
## Example
If the current time (hour) is less than 20:00, output "Good day" on the element with id="demo":
```javascript
var time = new Date().getHours();
if (time < 20) {
document.getElementById("demo").innerHTML = "Good day";
}
Output result:
Good day
[Try it Β»](#)
This page contains more examples at the bottom.
* * *
## Definition and Usage
The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code is executed.
The if/else statement is part of JavaScript's conditional statements, which are used to perform different actions based on different conditions.
In JavaScript, we can use the following conditional statements:
* **if statement** - Use this statement to execute a block of code only if a specified condition is true.
* **else statement** - Executes a block of code if the condition of the if statement is false.
* **else if statement** - Specifies a new condition to test if the first condition is false.
* **(#) statement** - Selects one of many code blocks to be executed.
* * *
## Browser Support
| Statement | | | | | |
| --- | --- | --- | --- | --- | --- |
| if/else | Yes | Yes | Yes | Yes | Yes |
* * *
## Syntax
The **if** statement specifies a block of code to be executed if the condition is true:
```javascript
if (_condition_) {
_The code block to be executed if condition is true_}
The **else** statement specifies a block of code to be executed if the condition is false:
```javascript
if (_condition_) {
_The code block to be executed if condition is true_} else {
_The code block to be executed if condition is false_}
The **else if** statement specifies a new condition if the first condition is false:
```javascript
if (_condition1_) {
_The code block to be executed if condition1 is true_} else if (_condition2_) {
_The code block to be executed if condition1 is false and condition2 is true_
} else {
_The code block to be executed if condition1 is false and condition2 is false_}
## Parameter Values
| Parameter | Description |
| --- | --- |
| _condition_ | Required. An expression that is evaluated to true or false |
## Technical Details
| JavaScript Version: | 1.0 |
| --- |
* * *

## More Examples
## Example
If the time is less than 20:00, generate a "Good day" greeting, otherwise output "Good evening":
```javascript
var time = new Date().getHours();
if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
The output of the greeting is:
```javascript
var d=new Date(); var time=d.getHours(); if (time<20) { document.write("Good day"); } else { document.write("Good evening"); }
[Try it Β»](#)
## Example
If the time is less than 10:00, output "Good morning" greeting; if the time is less than 20:00, output "Good day" greeting; otherwise output "Good evening":
```javascript
var time = new Date().getHours();
if (time < 10) {
greeting = "Good morning";
} else if (time < 20){
greeting = "Good day";
} else{
greeting = "Good evening";
}
The output of the greeting is:
```javascript
var d=new Date(); var time=d.getHours(); if (time<10) { document.write("Good morning"); } else if (time<20) { document.write("Good day"); } else { document.write("Good evening"); }
[Try it Β»](#)
## Example
Change the font size of the first
element in the document with id equal to "myDIV":
```javascript
var x = document.getElementsByTagName("DIV");
if (x.id == "myDIV") {
x.style.fontSize = "30px";
}
[Try it Β»](#)
## Example
Change the src attribute of an
![]()
element when the user clicks on it:

```javascript
function changeImage() {
var image = document.getElementById("myImage");
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
[Try it Β»](#)
## Example
Validate input data:
```javascript
var x, text;
// Get the value of the input field with id="numb"
x = document.getElementById("numb").value;
// If x is Not a Number or less than one or greater than 10, output "Input Not Valid"
// If the value of x is between 1 and 10, output "Input OK"
if (isNaN(x) || x 10) {
text = "Input Not Valid";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
[Try it Β»](#)
* * *
## Related Pages
JavaScript Tutorial: [JavaScript If...Else Statements](#)
JavaScript Tutorial: (#)
* * JavaScript Statement Reference Manual](#)