When you're coding in JavaScript, one of the fundamental things to grasp is the JavaScript if else statement. It is used to make your code adapt to specific conditions that you youself specify.
In this tutorial, you will learn all about its purpose and syntax. Using JavaScript if else statements will make your code more flexible and user friendly.
Contents
JavaScript if else: Main Tips
if,else ifandelseare conditional statements. They run code when a condition is met.- JavaScript
ifstatement runs a code block if a specific set condition istrue. - JavaScript
elsestatement runs a block of code if theifstatements condition isfalse. - JavaScript
else ifstatement runs a code block if the condition ofifisfalsebut an extra condition proves to betrue.
Choosing and Writing Statements
if JavaScript statement runs a block of code if a specific set condition is true.
If that same condition turns out to be false, JavaScript else statement runs a block of code. In this case, an extra condition might be set using JavaScript else if statement. It will run a block of code if the new conditions returns true.
If both if and else if statements aren't true, JavaScript else statement will run its block of code.
Sounds a bit complicated? Let's see a simple example:
function exampleFunction() {
var number = 15;
var text;
if (number < 15) {
text = "Number is less than 15";
} else if (number == 15){
text = "Number is equal to 15";
} else {
text = "Number is greater than 15";
}
document.getElementById("test").innerHTML = text;
}
As you try this example in the code editor, try changing the value in the second line and rerunning the code to see the differences.
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Examples
As you can see, the syntax for the JavaScript if else statement is really quite simple. Still, memorizing it might not be the same as understanding how to use it in practice.
We will now analyze three examples of using JavaScript if else statement in real code. The Try it Live buttons will allow you to open each of them in a code editor and play around with them a little so you get a better idea of the concept.
Example 1
In this example, we use JavaScript to check what time it is - to be more precise, whether it's before 8pm, in which case the function will return a "Good day!", and if it's a later hour, it will return "Good evening!".
We assign the time variable a date object with a method that gets the current hour. Then, using if statement JavaScript checks whether the current hour is earlier or later than 20:
function exampleFunction() {
var time = new Date().getHours();
var text;
if (time < 20) {
text = "Good day!";
} else {
text = "Good evening!";
}
document.getElementById("test").innerHTML = text;
}
Example 2
In the following example, we change the JavaScript if statement's condition so it checks if the hour is earlier than 10, in which case it outputs "Good morning!". In addition to that, we now add JavaScript else if statement, which checks if the hour is earlier than 20. If the first condition of if is false, and if the condition of else if is met, we output "Good day!".
Finally, JavaScript else statement returns "Good evening!" if both of the previous conditions are false:
var time = new Date().getHours();
if (time < 10) {
document.write("Good morning!");
} else if(time < 20) {
document.write("Good day!");
} else {
document.write("Good evening!");
}
Example 3
In the following example, we have a script, which takes the input of an HTML input field and checks its value through if and else if statements. If a user inputs c, the text states they entered the correct value.
JavaScript else if statement condition checks if you enter b or d. If that is true, the function returns that you are close to the right answer, because these letters are next to c in the alphabet.
If you enter any other value, JavaScript else statement informs you that you entered a wrong answer:
var letter = document.getElementById("my_input").value;
var text;
if (letter === "c") {
text = "Woo wee! You got it!";
} else if (letter === "b" || letter === "d") {
text = "Close enough, champ!";
} else {
text = "Awh... Not even close, man.";
}
document.getElementById("test").innerHTML = text;
JavaScript if else: Summary
- You can use JavaScript
ifstatement to check for a specific condition and execute some code if the condition is met. - If it is not, you can use
else ifstatement to check for an addition condition and execute some code if it is met. - You can use JavaScript
elsestatement to execute some code if none of the conditions are met.