Register to get access to free programming courses with interactive exercises

Logic JavaScript fundamentals

Since school we know not only arithmetic, but also comparative operations. For example, 5 > 4. This sounds like the question, "Is 5 greater than 4?". In this case, the answer is yes. In other cases, the answer may be no, for example 3 < 1.

Comparison operations are not tied to numbers. You can compare almost anything, such as strings.

Every time we access a site, there is a comparison of entered login and password with those that are in the database. And only if they are valid, we are allowed in to authorize.

Programming languages have adapted all mathematical comparison operations almost unchanged. The only major difference is the equality and inequality operators.

In mathematics, the usual equals = is used for this, but this is not common in programming. In many languages, the symbol = is used to assign values to variables, so we took == or === for comparison.

A list of comparison operations in JavaScript:

  • < less than
  • <= less or equal
  • > more than
  • >= more or equal
  • === equal
  • !== not equal

A small note: for equality and inequality, there are also operators == and !=, which we will not use for now. We will return to those in future lessons.

Logical operations

A logical operation like 5 > 4 or password === text is an expression, and its result is a special value true or false. This is a new data type for us — boolean. It contains just these two values:

const result = 5 > 4;
console.log(result); // => true
console.log('one' !== 'one'); // => false

Along with strings, numbers and rational numbers, boolean is one of the primitive data types in JavaScript.

We will try to write a primitive function which takes as input a child's age and determines whether the child is an infant or not. Infants are defined as children less than a year old:

const isInfant = (age) => age < 1;

We take advantage of the fact that any operation is an expression, so the only line of the function we write is "return the value that results from the comparison age < 1".

Depending on the argument, the comparison will either be true or false, and return will return that result:

const isInfant = (age) => age < 1;

isInfant(3); // false

Now let's check for six months old:

isInfant(0.5); // true

Predicates

Functions like isInfant() are called predicates. Predicate functions answer a question and always return either true or false.

Predicates in all languages have a special name for easy identification. In JavaScript, predicates usually start with, but are not limited to, the prefix is, has, or can. Examples:

  • isInfant() - "is an infant?"
  • hasChildren() - "are there children?"
  • isEmpty() - "is it empty?"
  • hasErrors() - "are there errors?"

A function can be considered a predicate only if it returns a boolean.

Let's write another predicate function. It takes a string and checks if it is 'Castle':

const isCastle = (type) => type === 'Castle';

isCastle('Sea'); // false

Combining logical operations

Logical operations are expressions. This means that we can combine logical operations with other expressions.

For example, we want to check if a number is even (a multiple of two). In programming we use this approach:

  • Check for the remainder of a division by 2
  • If the remainder is 0, then the number is even
  • If the remainder is not 0, then the number is odd

The remainder of division is a simple but very important concept in arithmetic, algebra, and even in number theory and cryptography. The idea is simple: divide a number into several equal groups, and if something remains at the end, that is the remainder of the division.

We divide the candy equally among the people:

  • 7 candy, 2 people: 2 x 3 + a remainder of 1. So 7 is not a multiple of 2
  • 21 candies, 3 people: 3 x 7 + remainder 0. So 21 is a multiple of 3
  • 19 candies, 5 people: 5 x 3 + remainder 4. So 19 is not a multiple of 5

The % operator calculates the remainder of division (not to be confused with division):

7 % 2;  // 1
21 % 3; // 0
19 % 5; // 4

// Parity check

10 % 2 // 10 is even, the remainder is 0
9 % 2  // 9 is odd, the remainder is 1

Let's write a function to check the parity:

const isEven = (number) => number % 2 === 0;

isEven(10); // true
isEven(3);  // false

In one expression, we have combined the logical operator === (equality check) and arithmetic operator %.

Arithmetic operations take precedence over logical ones. So, first the arithmetic expression number % 2 is calculated, then the result participates in a logical comparison.

In words, this can be interpreted as "Calculate the remainder after dividing number by 2 and compare whether the remainder equals zero; then return the result of the equality check".

Another example: let's write a function that takes a string and checks whether the first letter is capitalized.

Algorithm:

  • Get and write to a variable the first character from the parameter string
  • Compare whether the character equals its large (capital) version
  • Return the result
const isFirstLetterInUpperCase = (string) => {
    const firstLetter = string[0];
    return firstLetter.toUpperCase() === firstLetter;
};

isFirstLetterInUpperCase('marmont'); // false
isFirstLetterInUpperCase('Robb');    // true

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY