How To Use Shorthand For If/else Statement In JavaScript

There are many ways to use shorthand for if/else statement in JavaScript. Below, I will show you the ways to use some operators in JavaScript and help you understand them through a few specific examples.

Use shorthand for if/else statement in JavaScript

Use Conditional (Ternary) Operator  

This method is used by many programmers when using shorthand for the if/else statement because it is easy to understand and relatively simple to implement.

Syntax:

(condition) ? x : y

Parameters:

  • x: is the statement used when your conditional is true.
  • y: is the statement used when your conditional is false.

Example:

var condition = false;
condition ? console.log("condition is true") : console.log("condition is false");

Output:

condition is false

In the above example, we see that I have created the condition variable as false and used the Conditional (Ternary) Operator to check its value if it is true. If it is, it will output the string “condition is true”, but here, the variable condition is false, so the return result is not “condition is true” but “condition is false”.

Conditional (Ternary) Operator similar to If can test some complex conditionals. It is the most efficient shortening of if statements.

Use || and ?? Operator

|| Operator

Syntax:

leftExpr || rightExpr

Instead of using the If/else statement to check if its value is ‘, null, false, 0, or undefined, if yes, we will use that value; otherwise, we will change it to our value. we have given later || Operator.

Example:

// Example with Boolean
var boolean = true;
var boolean2 = false;

// Example with Number
var number = 0;
var number2 = 3;

// Example with String
var str = "";
var str2 = "String";

// Example with null and undefined
var value = null;
var value2 = undefined;

console.log(boolean || "not found");
console.log(boolean2 || "not found");
console.log(number || "not found");
console.log(number2 || "not found");
console.log(str || "not found");
console.log(str2 || "not found");
console.log(value || "not found");
console.log(value2 || "not found");

Output:

true
not found
not found
3
not found
String
not found
not found

Through the above example, we see that when we use it to replace the If statement to check if the variable has a value, we output the value or perform a message to the user.

?? Operator

Syntax:

leftExpr ?? rightExpr

This is considered a case of || Operator when the value is null or undefined, excluding cases like 0,””, false, will take the correct value and vice versa.

Example:

// Example with Boolean
var boolean = true;
var boolean2 = false;

// Example with Number
var number = 0;
var number2 = 3;

// Example with String
var str = "";
var str2 = "String";

// Example with null and undefined
var value = null;
var value2 = undefined;

console.log(boolean ?? "not found");
console.log(boolean2 ?? "not found");
console.log(number ?? "not found");
console.log(number2 ?? "not found");
console.log(str ?? "not found");
console.log(str2 ?? "not found");
console.log(value ?? "not found");
console.log(value2 ?? "not found");

Output:

true
false
0
3

String
not found
not found

We see the return result is almost || Operator, but in cases where the variables’ values are ‘, 0, false, then ?? Operator returns a different value.

Summary

In this article, I have helped you with some ways to use shorthand for if/else statement in JavaScript. Try to follow me. It won’t be difficult anymore. I hope this article will be for you and your program. Good luck.

Leave a Reply

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