AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

JavaScript If…Else


JavaScript Tutorial » JavaScript If…Else

JavaScript Conditional Statements

There are several conditional statements in JavaScript

1. if statement,
2. if...else statement,
3. if...else if....else statement,
4. switch...case statement


if Statement - example


if statement is used to execute a block of code only if the specified condition is true.

Example: if statement syntax
if(condition) {
// Code to be executed
}


Example:
<script>
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6

if(dayOfWeek == 5) {
alert("Have a nice weekend!");
}
</script>


if...else Statement - example


if...else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false.

Example: if .. else statement syntax
if(condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}


Example:

<script>
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6

if(dayOfWeek == 3) {
alert("Have a nice weekend!");
} else {
alert("Have a nice day!");
}
</script>

Output:
Have a nice day!

 

if...else if...else Statement, example

The if...else if...else a special statement that is used to combine multiple if...else statements.

Example: if...else if...else syntax
if(condition1) {
// Code to be executed if condition1 is true
} else if(condition2) {
// Code to be executed if the condition1 is false and condition2 is true
} else {
// Code to be executed if both condition1 and condition2 are false
}


Example: a is defined as a variable, and a is given the value 15:

<script>
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6

if(dayOfWeek == 5) {
alert("Have a nice weekend!");
} else if(dayOfWeek == 0) {
alert("Have a nice Sunday!");
} else {
alert("Have a nice day!");
}
</script>





JavaScript If…Else string contains, or, undefined, not null, and, not, checkbox is checked, has class, shortheand: online, if, example, ternary, condition, inline, alternative, break, ejemplo
JavaScript If…Else - javascript tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.