温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/if-else-statement-javascript/
点击访问原文链接

How to Use the JavaScript if-else Statement

How to Use the JavaScript if-else Statement Menu Sign in now Close Trending Submenu Productivity Android Smart TVs Networking Windows 11 Entertainment PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
How to Use the JavaScript if-else Statement Free image from pixabay Follow Followed Like Link copied to clipboard Add us on By  Idowu Omisola Published Aug 9, 2021, 11:45 AM EDT Idowu took writing as a profession in 2019 to communicate his programming and overall tech skills. At MUO, he covers coding explainers on several programming languages, cyber security topics, productivity, and other tech verticals. Idowu holds an MSc in Environmental Microbiology. But he sought out values outside his field to learn how to program and write technical explainers, enhancing his skill set. And he hasn't looked back since then. Sign in to your MakeUseOf account

Conditional statements inject logic into your program, and their application are limitless. If you're still new to JavaScript, mastering how to use the "if-else" statement lets you specifically instruct your program on how it should function.

Using conditions in JavaScript is easy. Let's get started.

How to Use Conditions in JavaScript

Like many other programming languages, conditional statements in JavaScript start with the if statement.

Here's what its syntax looks like:

if(condition){
// some actions;
}

If the condition within the parenthesis is true, then your program will execute the actions within the curly brace.

The else statement then comes in if you want to avoid a blank output when the if condition returns a false result:

if(condition){
// some actions;
}else{// execute something else}

Note that else doesn't accept a defined condition. Instead, it executes its designated actions only if a previous condition is false.

And if you want to check other conditions before you use an else, this is where the else if statement comes in handy. It's similar to how you use conditional statements in Python. It's an "elif" and not "else if" in Python, though.

To use the else if statement in JavaScript, the conditional syntax becomes:

if(condition){
// some actions;
}else if(another condition){
// execute another action
}else{// execute something else}

you can also use as many else if statements as you want:

if(condition){
// some actions;
}else if(another condition){
// execute another action
}else if(a third condition){
// perform another action
}else{// execute something else}

Now let's see some practical examples that combine these conditional statements:

var name = "idowu";
if(name== "MUO"){
console.log("Hello"+ " "+ name);
}else{
console.log("Hello everyone");
}
Output: Hello everyone

The example above executes the action within the else statement because the value of name isn't MUO.

Related:What Is JavaScript and How Does It Work?

Now let's see an example code that uses the three conditional statements.

The code below logs a greeting for the name at index zero only:

var names = ["MUO", "Jane", "Idowu"];
if(names[0]=="Jane"){
console.log("Hello"+ " " +names[0]);
}else if(names[0]=="Idowu"){
console.log("Hello"+ " " +names[0]");
}else if(names[0]=="MUO"){
console.log(Hello"+ " " +names[0]);
}else{
console.log("You didn't get the index");
}

Output: Hello MUO 
JavaScript Conditions With Ternary Operator

You can also declare JavaScript conditions using the ternary operator.

The general syntax looks like this:

var x = condition ? operation one : operation two

The question mark (?) checks the validity of the condition. If this is true, it executes operation one. Otherwise, it heads over to operation two.

Here's a practical example of how to use the ternary operator with an if-else statement:

var name = "Idowu";
var checkName =
name == "MUO" ? console.log("Hello"+ " "+ name) : console.log("Hello everyone");
Output: Hello everyone

And if you want to check more conditions using JavaScript ternary operator (similar to else if):

var temperature = 25
var checkTemperature =
tempOutput: Moderate

Here's a rewrite of the last code snippet in the previous section using ternary operation:

var names = ["MUO", "Jane", "Idowu"];

var checkIndex =

names[0] == "Jane" ? console.log("Hello"+ " " +names[0]) :

names[0] == "Idowu" ? console.log("Hello"+ " " +names[0]) :

names[0] == "MUO" ? console.log("Hello"+ " " +names[0]) :

console.log("You didn't get the index");

Output: Hello MUO

Note: Although it's straightforward, you might not want to use the ternary operation with nested conditions as it can confuse you. But it's still a neat way to express conditional statements if you know your way around it.

Control Your JavaScript With Conditions

Like every other programming language, a mastery of conditional statements in JavaScript lets you perform limitless logical operations. Consequently, you're in control of how your program behaves.

That said, whether you decide to use plain "if-else" statements or the ternary operation, ensure that your code is readable and easy to understand.

Close
Recommended 10 Tips for Writing Cleaner & Better Code I changed how I prompt NotebookLM and my audio overviews immediately sounded more interesting Stop using your TV's built-in speakers if you actually care about movies Siri’s big AI upgrade is actually just Google in a trench coat Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

How to Use the JavaScript if-else Statement,AI智能索引,全网链接索引,智能导航,网页索引

    The if-else statement is your first step towards programming logic into your applications.