JavaScript If Then Else ElseIf Statements

How to work with JavaScript if and else if conditional statements.

JavaScript if statements, is used to execute fragments of code, depending on which condition given by the developer has been meet. JavaScript if's will check for a given condition, and run the code provided if the condition has been met, otherwise it will run the code placed in the else section of the script.

JavaScript also allows the inclusion of so called else if statements, which will be executed if the condition in the preceding if statement wasn't met.

This Tutorial shows a message to the user, using the alert function, depending on what condition has been met. The alert function is used to show a small message box, which can be useful for script examples.

JavaScript if

The below is an example of a simple if check, without any else or else if statements. These checks can be useful on a number of occasions, mainly when you can predict the current state of a variable. For example, if a variable only has two possible values of "1" and "2", then it would be safe to assume that the current value is "2", given that it wasen't found to be "1".

<script type="text/javascript">
var number = 1;
if (number == 1) {
  alert('The number variable was checked, and found to be equal 1');
}
  // Place the rest of your script after this comment, which is ignored by the browser.
</script>

Brugbart Vision!

Javascript if else

The below script will check if the variable called "number" is equal to 1.

<script type="text/javascript">
var number = 1;
if (number == 1) {
  alert('The number variable was checked, and found to be equal 1');
} else {
  alert('The number variable was checked, and found NOT to be equal 1. The real real value is: '+number);
}
</script>

Brugbart Vision!

Javascript else ifs

Its easy to include else if checks in JavaScript. Keep in mind that else ifs are executed only if the first condition checked for wasn't met.

<script type="text/javascript">
var number = 77;
if (number == 1) {
  alert('The number variable was checked, and found to be equal 1');
} else if (number == 2) {
  alert('The number variable was checked, and found to be equal 2. The real real value is: '+number);
} else {
  alert('The number variable was checked, and found NOT to be equal 1 or 2. The real real value is: '+number);
}
</script>

Brugbart Vision!

Javascript Operators

Noticed how we used two equal signs rather then just one? This is because of the way comparison works in JavaScript. Two equal signs simply means "equal to", while three equal signs mean "exactly equal to".

Comparing a string to an integer using two equal signs would return true, but using three would return false. This is because 3 equal signs compares the type as well as the content of a variable.

Comparison Operators

Operator:Description:
==Equal to
!=Not Equal to
===Exactly equal to (Both type as well as the content)
<Less then
<=Less then or Equal to
>Greater than
>=Greater than or Equal to

Logical Operators

You can use these operators to provide multiple conditions to your if statements.

Operator:Description:
&&And
||Or
!Not

Using the And Operator

An example of how to include the And Operator in your if statements

<script type="text/javascript">
var number = 1;
var string = 'Brugbart rocks!';
if ((number === 1) && (string === 'Brugbart rocks!')) {
 alert(string);
}
</script>

Brugbart Vision!

Using the Or Operator

An example of how to include the Or Operator in your Scripts

<script type="text/javascript">
var number = '1';
var string = 'Brugbart rocks!';
if ((number === 1) || (string === 'Brugbart rocks!')) {
 alert(string);
}
</script>

Brugbart Vision!

The number variable above was decleared as a string, so the first condition is not met, but the second condition in the statement is met.

Using the Not Operator

<script type="text/javascript">
var number = 2010;
if (number != 1) {
 alert('The number was not 1..');
} else {
 alert('The number is still 1');
}
</script>

Brugbart Vision!

Post comment

Links that you insert are not nofollowed, but will be removed by admins if they are considered spam.

[url=Absolute URL for page]TITLE[/url]

You should insert code boxes around code examples, which will be automatically syntax highlighted.

[code1 html|css|javascript|php|sql]Your Code Here[/code1]

You may want to read our Privacy Policy before submitting your comment.