About operators
JavaScript can perform mathematical operations. They are performed by logical operators and comparison operators.
These are:
In addition, you can also use standard mathematical operators such as: +, -, *, /.
In JavaScript, there is a modulo operator (remainder of division). It is extremely useful when you want to check if the element is even. This operator is described by the percent sign (%).
9 % 2; // will return 1, because 9/2 is 4, and 1 is the remainder
Excercise 1
In the JavaScript code you will find several variables containing the result of comparison of numbers, i.e. logical values (boolean) known to you from the previous lesson. For better readability, comparison operations are in brackets. Replace equality signs with the appropriate operators so that the results below return true in each case. Go to the first exercise:
Tip:
var exN = (5 <= 8); // returns true, because 5 is less than or equal to 8
var exN = (5 >= 8); // returns false, because 5 is not greater than nor equal to 8
See the result of the exercise (click here to see the result)
Excercise 2
This task is analogous to the previous one. Replace signs of inequality in variables with appropriate operators so that the equations below return false in each case.
Proceed to the second exercise: Exercise 2
See the result of the exercise click here to see the result)
Excercise 3
Moving on to more complicated topics, we introduce logical operators. Let's discuss the example below:
var exN = (5 <= 8) && (1 > 3);
The value in the exN variable will be false. Why? Because the computer will do the following:
• First, it will calculate the result of the equation (5 <= 8). The result will be true. This value will be remembered for a while.
• Then it will calculate the result of the equation (1 > 3). This time, the result will be false. This value will be remembered for a while.
• At the end, it will calculate the result of the equation true && false (logical values have been remembered in previous steps). The result will be false, and this value will be saved to the variable.
Let's get to the exercise: Exercise 3. In the JavaScript code replace the equality or inequality signs with appropriate operators, so that the equations return true in each case. Do not change logical operators!
See the result of the exercise (click here to see the result)
Excercise 4
This task is analogous to the previous one. In JavaScript code, replace the operators with appropriate ones so that the results below return false in each case.
Go to the exercise: Exercise 4
See the result of the exercise (click here to see the result)