Expressions in JavaScript is sort of a Maths formula whose value is computed by applying basic math rules. Similarly **Logical Expressions are those JavaScript Expressions which are based upon Mathematical Logic AND, OR**. In JS for these logical operations &&, ||, ! symbols are used. Moreover if your aware of Boolean Algebra then it would be super easy for you to understand Logical Expressions in JavaScript.

Logical Expressions are just combination of two or more relational expressions combined together to make a single more complex expression

Operator | Symbol | Meaning |
---|---|---|

AND | && | Both of operands need to be true for final result to be true |

OR | || | If either of operand is true then result will be true |

NOT | ! | Turns true to false and false to true |

Nullish Coalescing | ?? | If we have x = a??b expression, then JavaScirpt will assign x value of a if a is defined or b if its defined. If none of a/b is defined then x will be assigned undefined value |

**Explanation of Logical Expressions in JavaScript**

*In case your not aware of what does it mean by Expression in JavaScript, then don’t worry. I got you covered here on this website, I’ve put together a long article for you explaining everything about JavaScript Expressions – You can see that article here – What are JavaScript Expressions?.*

**x > 10 && x < 15 // Means x should be greater than 10 but less than 15, both exclusive (x can be 11, 12, 13, 14). If x is indeed that then this expression(x > 10 && x < 15) will evaluate to true**.

Similarly other logic operators(OR, NOT) work as I’ve described above for &&.

Table of Contents

## Logical AND Operator(&&) in JavaScript

For better understanding how does Logical AND Operator works in JavaScript. Let’s first breakdown what exactly is meaning of **AND** in terms of Mathematical Logic.

**What is meaning of AND Logic? It means if both of values are true then whole expression evaluates to true, otherwise to false.**

This is fundamental concept of AND logic and in context of JavaScript this logic could be understood as following –

**If both operands are boolean or evaluate to boolean**

If a JavaScript expression have both of its operands either as boolean or an expression evaluating to boolean then simple Mathematical Logic is applied to get to final result.

Like **x && y** will evaluate to true, if in case both x, y are true booleans or just exist/evaluate to true.

**If Operands are objects or functions**

In case of having operands itself as some object/function JavaScript need to compute operands first and then do logical AND. Let’s understand this with an example – **var car = { name : “Tesla Model S” };var x = null;**

**car && car.name**; will evaluate to true as both operands values exists

**car && x;**evaluates to false as one of operand does not have any value

Just make that you don’t put complex expressions like functions as right hand side operand of AND Logical Operator as this may never be evaluated.

## Logical OR Operator(||) in JavaScript

Before understanding how does Logical OR Operator works in JavaScript. Let’s see what’s meaning of OR in Mathematical logic.

What is meaning of OR logic? It means if any one of values is true then whole expression will be true.

**a || b expression can be true if either of a or b is true. Meaning that for an expression to be true just one value needs to be true, no matter whether other value is false/true.** Owing to this for OR logical Operator Expression, JavaScript Engine just computes left hand side operand, if it’s true result would be true if it’s not true then it would evaluate right side operand.

**var car_model = tesla_model || toyota_model || honda_model;for the above expression car_model will get value of any model moving from left to right whichever has a value first.**

## Logical NOT Operator(!) in JavaScript

**Logical NOT is a simple operator as just inverts boolean, for example if a value is true then NOT Operator will turn it to false or vice-versa.**

!x would be true if x is false

!x would be false if x is true

It’s most simplest JavaScript logical Operator, but it quite useful in JavaScript often used where you need to reverse values.

## Nullish Coalescing Operator(??) in JavaScript

**The nullish coalescing operator ( ?? ) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined , and otherwise returns its left-hand side operand.** Let’s understand this with an example –

const foo = null ?? ‘Computer Science Hub’;

foo will be assigned ‘Computer Science Hub’

const coding = 0 ?? 362;

coding will be assigned 0 as it’s not null or undefined

## Conclusion

Logic is quite important part of programming as it helps to branch out code and helps in making code structure by putting together it as code blocks. So it’s quite important to better understand logic for becoming a better programmer.

In case you just want to ask something about Logical Operators in JavaScript or in general about JavaScript Programming Language, then leave a comment down below, I will try to reply you as soon as possible.

Also in case your learning **JavaScript Programming Language** then for you, I’ve put together many useful articles on this website. You can check those out here – JavaScript Computer Science Hub.