DEV Community 👩‍💻👨‍💻

Cover image for JavaScript basics logical operators
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

JavaScript basics logical operators

In today's article, we'll be looking at JavaScript logical operators.
JavaScript comes with three logical operators being and, or and not.

Check out the below table of the basic use cases.

Operator Logic Example
&& And a = true && b = false
`\ \ `
! Not let a = true
!a // false

Let's have a more detailed view of each of these logical operators in JavaScript.

JavaScript And operator

The and operator can be used to assess if two expressions are met.

The syntax is as follows:

expression && expression;
Enter fullscreen mode Exit fullscreen mode

Some examples might be:

const a = true;
const b = 5;
a === true && b > 3;
// true
Enter fullscreen mode Exit fullscreen mode

The return will always be an evaluation in the form of a boolean. We are returning either true if both expressions are met or false when one or both fails.

The operator is often used with a if...else statement to perform an action based on the logic.

JavaScript Or operator

Much like the and operator, we can also use the or operator, which is used by placing two pipes like this: ||.
This operator is used to evaluate if both or one of the expressions is met.

expression || expression;
Enter fullscreen mode Exit fullscreen mode

Let's say we want to check if a is true or b is greater than 3. We don't need both to be truthy, just one.

const a = true;
const b = 1;
a === true || b > 3;
// true
Enter fullscreen mode Exit fullscreen mode

The above example will still return true since it will succeed to be correct.

JavaScript Not operator

This is a bit of a funny one, as it is used to invert the value of a boolean.

So let's say we have a true boolean and want to convert it to false:

let a = true;
!a;
// false
Enter fullscreen mode Exit fullscreen mode

However, using this in an if statement will evaluate if the condition is NOT met.

let a = true;

if (!a) {
  // It will never get here now
}
Enter fullscreen mode Exit fullscreen mode

However, we mainly use this to convert a value to the opposite boolean value.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (0)

Tired of sifting through your feed?

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.