• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript if else Shorthand Statement

JavaScript if else Shorthand Statement

September 8, 2022 Leave a Comment

In JavaScript, we can write an if else statement in shorthand by using the conditional (ternary) operator.

(condition) ? option1:option2;

So if the condition in the code above is true, then the first option will be executed. Otherwise, the second option will run if the condition is false.

Let’s see some examples of this below.


First, let’s go over a very simple if-else statement setup.

if(condition){
  option1
} else {
  option2
}

Let’s show a simple example using an if-else statement and then use our shorthand if-else code after.

var number1 = 3;
var number2 = 4;
if( number1<number2 ){
  var result = "number1 is less than number2";
} else {
  var result = "number2 is less than number1";
}

console.log(result);

#Output
number1 is less than number2

And now the same example above with our shorthand code.

var number1 = 3;
var number2 = 4;
var result = (number1<number2) ? "number1 is less than number2":"number2 is less than number1";

console.log(result);

#Output
number1 is less than number2

You can see how we write less code using the shorthand if-else statement.

Now let’s switch the number values to see what will happen.

var number1 = 4;
var number2 = 3;
var result = (number1<number2) ? "number1 is less than number2":"number2 is less than number1";

console.log(result);

#Output
number2 is less than number1

Let’s take a look at one last example.

Here we will simply want to iterate over one large array of numbers and split the numbers into two separate arrays based on whether the number is even or odd.

The code to use to check if a number is even or odd is usually the if-else statement:

if ((num % 2) == 0){
  //Number is Even
} else {
  //Number is Odd
}

So note how we replace this with our shorthand if-else statement below.

var numsArray = [1,2,3,4,5,6,7,8,9,10];
var oddArray = [];
var evenArray = [];
for( var i = 0; i<numsArray.length; i++ ){
  ((numsArray[i] % 2) == 0) ? evenArray.push(numsArray[i]):oddArray.push(numsArray[i]);
}

console.log(oddArray);
console.log(evenArray);

#Output
[1, 3, 5, 7, 9]
[2, 4, 6, 8, 10]

Hopefully this article has been useful in helping you understand how to use JavaScript if else shorthand statement.

Other Articles You'll Also Like:

  • 1.  How to Clear a Textarea Field In JavaScript
  • 2.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 3.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 4.  How to Repeat a String in JavaScript
  • 5.  Using JavaScript to Check if Number is Divisible by Another Number
  • 6.  Swapping Images in JavaScript
  • 7.  Create Array of Zeros in JavaScript
  • 8.  JavaScript offsetTop – Get the Top Position of Element
  • 9.  Using JavaScript to Check if Array is Empty
  • 10.  How to Repeat Character N Times in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x