Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Logical Operators Explained | Performing Operations in JavaScript
Introduction to JavaScript

bookLogical Operators Explained

メニューを表示するにはスワイプしてください

Now, let's explore logical operations. Logical operations are used to manipulate and make decisions based on boolean values.

Logical Operators

There are three logical operators in JavaScript:

  • AND (&&);
  • OR (||);
  • NOT (!).

AND (&&)

The AND (&&) logical operator returns true only when both values it operates on are true. The result is false if any value is false.

1234
console.log(true && true); console.log(true && false); console.log(false && true); console.log(false && false);
copy

The AND (&&) operator requires all values to be true to return true.

OR (||)

The OR (||) logical operator returns true if at least one of the values is true. It returns false only when both values are false.

1234
console.log(true || true); console.log(true || false); console.log(false || true); console.log(false || false);
copy

The OR (||) operator returns false only when both values are false.

NOT (!)

The NOT (!) operator inverts the boolean value. It turns true into false and false into true.

12
console.log(!true); console.log(!false);
copy

Using Logical Operators

Logical operators are often used to combine conditions. For example, to check if two values are greater than 4 or greater than 1, you can use the AND (&&) operator.

1234
let a = 5, b = 3; console.log(a > 4 && b > 4); console.log(a > 1 && b > 1);
copy

Priority

Logical operations have a lower priority than comparison operations, which, in turn, have a lower priority than mathematical operations.

Note

Similar to other operations, you can use parentheses () to adjust the priority of execution.

1. What will be the output?

2. What will be the output?

3. What will be the output?

question mark

What will be the output?

正しい答えを選んでください

question mark

What will be the output?

正しい答えを選んでください

question mark

What will be the output?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  7
some-alt