Latest web development tutorials

JavaScript if / else statements

JavaScript Statements Reference Manual JavaScript Statements Reference Manual

Examples

If the current time (hours) is less than 20:00, the output of "Good day" in id = "demo" element:

var time = new Date () getHours ().;
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(condition) {
If the condition is true execute the code block
}

else statement specifies the code block executed if the condition is false:

if(condition) {
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) {
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


Examples

More examples

Examples

If the time is less than 20:00, to 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 is a greeting:

var d = new Date (); var time = d.getHours (); if (time <20) {document.write ( "Good day");} else {document.write ( "Good evening");}

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":

var time = new Date () getHours ().;
if (time <10) {
greeting = "Good morning";
} Else if (time <20) {
greeting = "Good day";
} Else {
greeting = "Good evening";
}

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"

Examples

Modify the document in the first id equal "myDIV" the <div> element font number:

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

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:

<Img id = "myImage" onclick = "changeImage ()" src = "pic_bulboff.gif" width = "100" height = "180">

<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:

var x, text;

// 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 JavaScript Statements Reference Manual