JavaScript Logical Operators

JavaScript-Logical-Operators-feature-image.jpg

In programming, we may need to make decisions, like displaying a message when the user logs in. That’s the place where JavaScript logical operators come in. JavaScript logical operators help in handling logic in our code. This operator allows users to check multiple conditions at once and make our programs smarter. There are different types of logical operators in JavaScript, and each of the operators has its own function. In this blog, you will learn all about JavaScript logical operators and how these operators are used in code with real-world examples.

Table of Contents:

Truthy and Falsy Values in JavaScript.

Before learning JavaScript Logical Operators, it is important to understand truthy and falsy values in JavaScript. Every value in JavaScript is either truthy or falsy when used in a condition. A truthy value is defined as the value that counts as true in a condition, while a falsy value is something that counts as false in a condition. For example, false, 0, -0, null, undefined, NaN, and ” “ are some values that are considered as falsy in JavaScript.

JavaScript Operators

Operators in JavaScript are defined as special keywords that are used to perform actions on some values. You can use JavaScript operators to compare values or add logic. There are various types of operators in JavaScript:

  • Arithmetic Operators (+, -, *, /)
  • Assignment Operators (=, +=, -=)
  • Comparison Operators (==, ===, !=, <, >)
  • Logical Operators (&&, ||, !)
  • Unary Operator (typeof, !)
  • Ternary Operator (condition ? value1 : value2)
  • Nullish Coalescing Operator (??)

What are Logical Operators in JavaScript?

Logical operators in JavaScript are used to make decisions based on more than one value. These operators give you a result based on the condition. In simple words, logical operators are used when you have to write two or more conditions. For example, sometimes you may want to allow someone to access a page only if they are logged in and verified. For this, you can use JavaScript logical operators.

Launch Your Web Development Career
Start Learning Now
quiz-icon

Types of Logical Operators

JavaScript logical operators are basically used to make a decision if you have more than one condition. There are four types of logical operators in JavaScript. Let’s discuss each of them one by one in detail:

1. Logical AND (&&) Operator

The JavaScript logical AND operator checks whether both conditions are true or not. If both conditions are true, the result is true. If even one of them is false, the result is false. It is represented by && in JavaScript.

Truth Table for Logical AND Operator in JavaScript:

First Condition (A)Second Condition (B)Result (A && B)
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Example: User is eligible to vote only if they are 18 years old and have a valid voter ID card.

Javascript

Output:

Logical AND operator

2. Logical OR (||) Operator

The JavaScript logical OR operator is used to check if at least one condition is true. If one or both conditions are true, the result is true. If both conditions are false, then the result is also false. It is represented as || in JavaScript.

Truth Table for Logical OR Operator in JavaScript:

First Condition (A)Second Condition (B)Result (A || B)
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Example: A movie theatre allows entry if a person is 18 or more than 18, or they are with a guardian.

Javascript

Output:

Logical OR operator

3. Logical NOT (!) Operator

The logical NOT operator reverses the value, which means if something is true, then the NOT operator makes it false, and if the value is false, then the NOT operator makes it true. It is represented by! symbol in JavaScript.

Truth Table for Logical NOT Operator in JavaScript:

Condition (A)Result (!A)
truefalse
falsetrue

Example: Check if a user is not logged in.

Javascript

Output:

logical NOT operator

4. Nullish Coalescing (??) Operator

The nullish coalescing JavaScript operator (??) refers to the operator that returns the right-hand value only if the left-hand value is null or undefined. It is mostly used for setting default values.

Left-hand ValueRight-hand ValueResult
null“Guest”“Guest”
undefined“Guest”“Guest”
“”“Guest”“”
0“Guest”0

Example: Setting display name if the username is null or undefined.

Javascript

Output:

Nullish Coalescing Operator

Best Practices for Using Logical Operators

Using JavaScript logical operators in your code allows you to write clean and readable code. Here are some best practices that you need to follow while using JavaScript logical operators:

  • If you want to work with JavaScript logical operators, then it is recommended to understand the truthy and falsy values in JavaScript
  • The nullish coalescing JavaScript operator (??) is more accurate than the JavaScript OR operator (||) in a case when you want to check for null or undefined, not for other falsy values like 0 or “”.
  • It is good to use parentheses if you are combining one or more logical operators for writing a condition.
  • Keep the logic simple, so that other users or developers can understand it easily.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

JavaScript logical operators are important for writing efficient code. These operators help you to make decisions and handle conditions. The JavaScript logical AND operator is used to make sure that two things are true. The JavaScript logical OR is used to check if at least one condition is true, and the nullish coalescing JavaScript operator is used to provide a default value. You will be able to write efficient code by understanding these logical operators, along with the truthy and falsy values in JavaScript.

To learn more about JavaScript Operators, check out this Web Development course and also explore JavaScript Interview Questions prepared by industry experts.

JavaScript Logical Operators – FAQs

Q1. What are JavaScript Operators?

JavaScript operators are some special keywords that allow you to do some actions like adding numbers, comparing values, and checking conditions.

Q2. How many types of operators are present in JavaScript?

JavaScript offers various types of operators, like arithmetic operators, assignment operators, comparison operators, unary operators, the ternary operator, and the nullish coalescing operator.

Q3. What are the logical operators?

Logical operators in JavaScript are used to make decisions based on more than one value.

Q4. Which is a ternary operator?

A ternary operator is a shorthand property used for writing conditional statements.

Q5. What are the different data types in JavaScript?

JavaScript offers various data types like String, Number, Boolean, Undefined, and Object.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner