The if statement in Javascript

By: Syed Fazal Emailed: 1674 times Printed: 2152 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

One of the most frequently used statements in JavaScript (and indeed, in many languages), is the if statement. The if statement has the following syntax:

if ( condition ) statement1 else statement2

The condition can be any expression; it doesn’t even have to evaluate to an actual Boolean value. JavaScript converts it to a Boolean for you. If the condition evaluates to true , statement1 is executed; if the condition evaluates to false , statement2 is executed. Each of the statements can be either a single line or a code block (a group of code lines enclosed within braces). For example:

if (i > 25)
   alert(“Greater than 25.”); //one-line statement
else {
  alert(“Less than or equal to 25.”); //block statement
}

You can also chain if statements together like so:

if ( condition1 ) statement1 else if ( condition2 ) statement2 else statement3

Example:

if (i > 25) {

  alert(“Greater than 25.”)

} else if (i < 0) {

  alert(“Less than 0.”);

} else {

  alert(“Between 0 and 25, inclusive.”);

}

Tip: It’s considered best coding practice to always use block statements, even if only one line of code is to be executed. Doing so can avoid confusion about what should be executed for each condition.


Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Syed Fazal
switch in Javascript
break and continue in Javascript
do-while, while, for and for-in loops in Javascript
The if statement in Javascript
Using && in Javascript
Using ! and !! in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
String Concatenation using Java

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
The if statement in Javascript
Show how many characters remaining in a html text box using javascript
Click to remove an element from a select html form using javascript
History and evolution of Javascript
Javascript Basics
List of Keywords and reverved words in Javascript
Using toString() in JavaScript to convert data types to String
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Typecasting in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
Using ! and !! in Javascript
Using && in Javascript
do-while, while, for and for-in loops in Javascript
break and continue in Javascript
switch in Javascript
Most Emailed Articles (in Javascript)
window.frames[i] in Javascript
Scrolling message on the status bar using Javascript
Verifying user input in JavaScript
List of Keywords and reverved words in Javascript
Typecasting in Javascript
The if statement in Javascript
call() and apply() methods in Javascript
Form validation using Javascript
Pause execution in JavaScript
Javascript Basics
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
do-while, while, for and for-in loops in Javascript
this keyword sample in Javascript
Password protect a web page using Javascript
Javascript to display client date and time on webpage