JavaScript: Logical operators
Logical expressions can be combined with one another, creating increasingly tricky checks. A good example is password validation. As you know, some websites require a password between 8 and 20 characters long during registration. Honestly, it's a strange restriction, but what can you do. In math, we would write 8 < x < 20 (where x is the length of a specific password), but in JavaScript this trick won't work. We will have to make two separate logical expressions and join them with a special "AND" operator:
The password is longer than 8 characters **AND** the password is shorter than 20 characters.Here is a function that takes a password and says whether it meets the conditions or not:
const isStrongPassword = (password) => {
const length = password.length;
return length > 8 && length < 20;
};
isStrongPassword('qwerty'); // false
isStrongPassword('qwerty1234'); // true
isStrongPassword('zxcvbnmasdfghjkqwertyui'); // false&& means "AND" (in mathematical logic this is called conjunction). The whole expression is considered true only when each operand is true — each of the component expressions. In other words, && means "both one and the other".
The precedence of this operator is lower than that of the comparison operators, so the expression works correctly without parentheses.
Besides &&, the operator || — "OR" (disjunction) is often used. It means "either one, or the other, or both". Operators can be combined in any number and in any sequence, but when && and || occur together, it's better to set precedence with parentheses. Below is an example of an extended function for determining password correctness:
const hasSpecialChars = (str) => /* checks whether the string contains special characters */;
const hasCapitalChars = (str) => /* checks whether the string contains capital letters */
const isStrongPassword = (password) => {
const length = password.length;
// Parentheses set the precedence. It's clear what relates to what.
return length > 8 && (hasSpecialChars(password) || hasCapitalChars(password));
};Another example. We want to buy an apartment that meets the conditions: an area of 100 square meters or more on any street OR an area of 80 square meters or more, but on the central street Main Street.
Let's write a function that checks the apartment. It takes two arguments: the area (a number) and the street name (a string):
const isGoodApartment = (area, street) => {
// Via a variable so the function isn't too long
const result = area >= 100 || (area >= 80 && street === 'Main Street');
return result;
};
isGoodApartment(91, 'Queens Street'); // false
isGoodApartment(78, 'Queens Street'); // false
isGoodApartment(70, 'Main Street'); // false
isGoodApartment(120, 'Queens Street'); // true
isGoodApartment(120, 'Main Street'); // true
isGoodApartment(80, 'Main Street'); // trueThe branch of mathematics that studies logical operators is called Boolean algebra. Below are the "truth tables" — you can use them to determine the result of applying an operator:
AND &&
| A | B | A && B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE |
| FALSE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
A couple of examples:
// true && true;
3 > 2 && 'wow'.startsWith('w'); // true
// true && false;
'start' === 'start' && 8 < 3; // falseOR ||
| A | B | A || B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | TRUE |
| FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE |
A couple of examples:
// true || true;
3 > 2 || 'wow'.startsWith('w'); // true
// false || false;
'start' === 'Start' || 3 < 3; // falseInstructions
Write a function isLeapYear(year) that returns true if the year is a leap year.
A year is a leap year if:
- it is divisible by 4, but not by 100, or
- it is divisible by 400.
isLeapYear(2000); // => true
isLeapYear(1900); // => false
isLeapYear(2024); // => true
isLeapYear(2023); // => falseTips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
JavaScript: Logical operators
Logical expressions can be combined with one another, creating increasingly tricky checks. A good example is password validation. As you know, some websites require a password between 8 and 20 characters long during registration. Honestly, it's a strange restriction, but what can you do. In math, we would write 8 < x < 20 (where x is the length of a specific password), but in JavaScript this trick won't work. We will have to make two separate logical expressions and join them with a special "AND" operator:
The password is longer than 8 characters **AND** the password is shorter than 20 characters.Here is a function that takes a password and says whether it meets the conditions or not:
const isStrongPassword = (password) => {
const length = password.length;
return length > 8 && length < 20;
};
isStrongPassword('qwerty'); // false
isStrongPassword('qwerty1234'); // true
isStrongPassword('zxcvbnmasdfghjkqwertyui'); // false&& means "AND" (in mathematical logic this is called conjunction). The whole expression is considered true only when each operand is true — each of the component expressions. In other words, && means "both one and the other".
The precedence of this operator is lower than that of the comparison operators, so the expression works correctly without parentheses.
Besides &&, the operator || — "OR" (disjunction) is often used. It means "either one, or the other, or both". Operators can be combined in any number and in any sequence, but when && and || occur together, it's better to set precedence with parentheses. Below is an example of an extended function for determining password correctness:
const hasSpecialChars = (str) => /* checks whether the string contains special characters */;
const hasCapitalChars = (str) => /* checks whether the string contains capital letters */
const isStrongPassword = (password) => {
const length = password.length;
// Parentheses set the precedence. It's clear what relates to what.
return length > 8 && (hasSpecialChars(password) || hasCapitalChars(password));
};Another example. We want to buy an apartment that meets the conditions: an area of 100 square meters or more on any street OR an area of 80 square meters or more, but on the central street Main Street.
Let's write a function that checks the apartment. It takes two arguments: the area (a number) and the street name (a string):
const isGoodApartment = (area, street) => {
// Via a variable so the function isn't too long
const result = area >= 100 || (area >= 80 && street === 'Main Street');
return result;
};
isGoodApartment(91, 'Queens Street'); // false
isGoodApartment(78, 'Queens Street'); // false
isGoodApartment(70, 'Main Street'); // false
isGoodApartment(120, 'Queens Street'); // true
isGoodApartment(120, 'Main Street'); // true
isGoodApartment(80, 'Main Street'); // trueThe branch of mathematics that studies logical operators is called Boolean algebra. Below are the "truth tables" — you can use them to determine the result of applying an operator:
AND &&
| A | B | A && B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE |
| FALSE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
A couple of examples:
// true && true;
3 > 2 && 'wow'.startsWith('w'); // true
// true && false;
'start' === 'start' && 8 < 3; // falseOR ||
| A | B | A || B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | TRUE |
| FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE |
A couple of examples:
// true || true;
3 > 2 || 'wow'.startsWith('w'); // true
// false || false;
'start' === 'Start' || 3 < 3; // falseInstructions
Write a function isLeapYear(year) that returns true if the year is a leap year.
A year is a leap year if:
- it is divisible by 4, but not by 100, or
- it is divisible by 400.
isLeapYear(2000); // => true
isLeapYear(1900); // => false
isLeapYear(2024); // => true
isLeapYear(2023); // => falseTips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import { expect, test } from 'vitest';
import f from './index.js';
test('logical operators', () => {
expect(f(2016)).toBe(true);
expect(f(2000)).toBe(true);
expect(f(2017)).toBe(false);
expect(f(2018)).toBe(false);
expect(f(1900)).toBe(false);
});Teacher's solution will be available in:
20:00
