JavaScript: Logical operators
You can combine logical expressions to create increasingly cleverer and more useful checks. One good example is password verification. As you know, some websites want a password of 8 to 20 characters on signup. Frankly, it's a weird restriction, but whatever, it is what it is. In math, we would write 8 < x < 20
(where x
is the length of a particular password), but that trick won't work in JavaScript. We would have to make two separate logical expressions and connect them with the special "AND" operator:
A password longer than 8 characters **AND** a password shorter than 20 characters.
Here's a function that takes the 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" (called a conjunction in mathematical logic). The whole expression is true only when every operand, which are all part of the compound expressions, is true. In other words, &&
means "both".
This operator's priority is lower than that of comparison operators, so the expression works correctly without parentheses.
Another widespread operator along with &&
is ||
— "OR" (disjunction). It means "one or the other, or both". Operators can be combined in any number and any sequence, but when &&
and ||
appear together, you should label priority with parentheses. Below is an example of an advanced function validating a password:
const hasSpecialChars = (str) => /* checks for special characters in the string */;
const isStrongPassword = (password) => {
const length = password.length;
// The parentheses set the priority, making it clear how each part is related
return (length > 8 && length < 20) || hasSpecialChars(password);
};
Another example. We want to buy an apartment that meets these conditions: an area of 100 square meters or more on any street OR an area of 80 square meters or more, but on Main Street
.
We'll write a function checking the apartment. It takes two arguments: the area (a number) and the street name (a string):
const isGoodApartment = (area, street) => {
// Via a variable to make sure the function is not 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'); // true
The area of mathematics dealing with logical operators is called Boolean algebra. The "truth tables" are shown below and can be used to figure out the result of an operator:
AND &&
A | B | A && B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE |
FALSE | TRUE | FALSE |
FALSE | FALSE | FALSE |
Few examples:
// true && true;
3 > 2 && 'wow'.startsWith('w'); // true
// true && false;
'start' === 'start' && 8 < 3; // false
OR ||
A | B | A || B |
---|---|---|
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
Few examples:
// true || true;
3 > 2 || 'wow'.startsWith('w'); // true
// false || false;
'start' === 'Start' || 3 < 3; // false
Instructions
Write a function, isLeapYear()
, to determine whether a year is a leap year or not. A leap year is a multiple of 400 (i.e. divisible without a remainder), or it is both a multiple of 4 and not a multiple of 100. As you can see, the definition already contains all the required logic, all we need to do is to put it into code:
isLeapYear(2018); // false
isLeapYear(2017); // false
isLeapYear(2016); // true
You can test multiplicity as follows:
// % - returns the remainder after dividing the left operand by the right one
// Check if number is a multiple of 10
number % 10 === 0
// Check if number is not a multiple of 10
number % 10 !== 0
The exercise doesn't pass checking. What to do? 😶
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.
In my environment the code works, but not here 🤨
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
My code is different from the teacher's one 🤔
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
I've read the lessons but nothing is clear 🙄
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Tips
Definitions
Logical operators are the AND (&&) and OR (||) operators, which allow you to create compound logical conditions.