Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript if/else Statements

JavaScript if/else Statements

JavaScript Statement Reference JavaScript statement reference manual

Example

If the current time (hour) is less than 20:00, output "Good day" to the element with id="demo":

var time = new Date().getHours();

if (time < 20) {

    document.getElementById("demo").innerHTML = "Good day";

}

Output result:

Try it »

More examples are included at the bottom of this article.


Definition and Usage

The if/else statement executes a block of code when the specified condition is true. If the condition is false, another block of code is executed.

The if/else statement is part of JavaScript conditional statements, which are used to perform different actions based on different conditions.

In JavaScript, we can use the following conditional statements:


Browser support

Statement
if/else Yes Yes Yes Yes Yes

Syntax

if The statement specifies the code block to execute when the condition is true:

if (

condition

) {

    If condition is true, execute this code block

}

else The statement specifies the code block to execute when the condition is false:

if (

condition

) {

    If condition is true, execute this code block

} else {

    If condition is false, execute this code block

}

else if The statement specifies a new condition when the first condition is false:

if (

condition1

) {

    If condition1 is true, execute this code block

} else if (

condition2

) {

    If condition1 is false and condition2 is true, execute this code block

} else {

    If condition1 is false and condition2 is false, execute this code block

}

Parameter value

Parameters Description
condition Required. An expression used for condition evaluation: 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":

var time = new Date().getHours();

if (time < 20) {

    greeting = "Good day";

} else {

    greeting = "Good evening";

}

The output of the greeting is:

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 a "Good morning" greeting; if the time is less than 20:00, output a "Good day" greeting; otherwise output "Good evening":

var time = new Date().getHours();

if (time < 10) {

    greeting = "Good morning";

} else if (time < 20) {

    greeting = "Good day";

} else {

    greeting = "Good evening";

}

The greeting output is:

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

Modify the first element in the document whose id equals "myDIV"

The font size of the element:

var x = document.getElementsByTagName("DIV")[0];

if (x.id == "myDIV") {

    x.style.fontSize = "30px";

}

Try it »

Example

Modify when the user clicks the image The src attribute of the element:

Try it »

Example

Validate the input data:

var x, text;

// Get the value of the input box with id="numb"

x = document.getElementById("numb").value;

// If x is not a number or x is less than 1 or greater than 10, output "Please enter a valid value"

// If the value of x is between 1 and 10, output "Input is correct"

if (isNaN(x) || x < 1 || x > 10) {

    text = "Please enter a valid value";

} else {

    text = "Input is correct";

}

document.getElementById("demo").innerHTML = text;

Try it »


Related pages

JavaScript tutorial: JavaScript If...Else Statements

JavaScript tutorial: JavaScript Switch Statements


JavaScript Statement Reference JavaScript statement reference manual