Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Логічні Оператори | Section
Основи JavaScript

bookЛогічні Оператори

Свайпніть щоб показати меню

When you need to make decisions in your code based on more than one condition, logical operators let you combine or modify these conditions. JavaScript gives you three main logical operators: AND (&&), OR (||), and NOT (!).
These operators are used to join multiple boolean expressions or to invert their values.

  • The AND operator (&&) returns true only if both conditions are true;
  • The OR operator (||) returns true if at least one of the conditions is true;
  • The NOT operator (!) flips the value of a condition: if a condition is true, ! makes it false, and vice versa.

You use these operators most often in if statements to control the flow of your program based on more complex logic.

123456789101112131415161718192021
const age = 20; const hasTicket = true; // Using AND (&&): both conditions must be true if (age >= 18 && hasTicket) { console.log("You can enter the event."); } else { console.log("Entry denied."); } // Using OR (||): at least one condition must be true const isMember = false; if (age >= 18 || isMember) { console.log("You qualify for a discount."); } // Using NOT (!): invert a condition const isBanned = false; if (!isBanned) { console.log("Access granted."); }
copy

When you combine logical operators, operator precedence determines the order in which they are evaluated. In JavaScript, the NOT (!) operator has the highest precedence, followed by AND (&&), and then OR (||).

This means expressions with ! are evaluated first, then &&, and finally ||. You can use parentheses () to group conditions and control the evaluation order.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 8

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 8
some-alt