Arithmetic Operators And Tricks In JavaScript By albro

Arithmetic Operators And Tricks

You can see arithmetic operators in JavaScript below:

  1. +: adding (var x = 100 + 50;)
  2. -: Subtract (var x = 100 - 50;)
  3. *: Multiply (var x = (100 + 50) * a;)
  4. **: Exponentiation (var z = x ** 2;)
  5. /: divide (var z = x / y;)
  6. %: Divide remaining (var z = x % y;)
  7. ++: Add one unit (x++;)
  8. --: Subtract one unit (x--;)

The "Exponentiation" operator is only supported in ECMAScript 6 and later, which was introduced in 2015.

For some of these operators, there are also predefined functions. For example, to reach a specific power, you can use the function Math.pow(x,y):

< !DOCTYPE html>
< html>
< body>
< h2>The ** Operator and Math.pow()< /h2>
< p id="demo1">< /p>
< p id="demo2">< /p>

< script> var x = 5;
document.getElementById("demo1").innerHTML = x ** 2;
document.getElementById("demo2").innerHTML = Math.pow(x,2);
< /script>
< /body> < /html>

Exponentiation Operator

The functional form of this statement takes two arguments; The first argument is the number or variable that needs to be exponentiated and the second argument is the desired exponent.

The percentage sign has nothing to do with percentages and so on. This operator shows you the remainder of a division. Consider the following example:

< !DOCTYPE html>
< html>
< body>
< h2>The % Operator< /h2>
< p id="demo">< /p>
< script> var x = 5; var y = 2; var z = x % y; document.getElementById("demo").innerHTML = z; < /script>
< /body> < /html>

The code output will be 1. Why? Because the remainder of dividing 5 by 2 is 1.

Remainder Operator

General tips when working with arithmetic operators:

  • In an arithmetic operation, the numbers are called operands and the sign that is one of the above signs is called operator. Don't be surprised if you see these somewhere with such names.
  • When using the division operator (/), be careful not to divide a number by zero! Dividing by zero makes no logical sense and the result will be infinity. You can see such an example in the code below:
< !DOCTYPE html>
< html>
< body>
< h2>The / Operator< /h2>
< p id="demo">< /p>
< script> var x = 5; var y = 0; var z = x / y; document.getElementById("demo").innerHTML = z; < /script>
< /body> < /html>

Division Operator

You may ask yourself, what sane person would do such a thing? The discussion is not here! My warning is to be careful when doing a real project.

For example, if you are writing code for a restaurant and dealing with prices, you will get lost in the code. If you are not careful, you may inadvertently come across such a case, because in real programming, it is rare to manually add, subtract, divide, etc. numbers.

Numbers are given to us in the form of dynamic variables (variables whose value may change) and in this case they will not be so clear. The solution to this problem is to use if and other types of conditional statements.

Does division by zero cause a problem for me?

According to Wikipedia and most reputable programming websites (and of course personal experience!) yes:

In computing, a program error may result from an attempt to divide by zero. Depending on the programming environment and the type of number (e.g. floating point, integer) being divided by zero, it may generate positive or negative infinity by the IEEE 754 floating point standard, generate an exception, generate an error message, cause the program to terminate, result in a special not-a-number value, a freeze via infinite loop, or a crash.

So your program can stop or get stuck in an infinite loop

Precedence

You may have seen the term Operator precedence in various educational websites. Operator precedence means which calculations and operators are executed first in a long calculation string. Pay attention to the following example:

var x = 100 + 50 * 3;

What will be the answer to the above code? The summary of this expression is 3 * 150 or 150 + 100? Which part is calculated first? Addition sign or multiplication sign?

This question depends on the preference of the operators. If you remember, we read in math that multiplication and division are preferred over addition and subtraction, so it is calculated first. So the answer is 250.

Now, what should we do if we want it to happen first? We have to raise its priority. how about Using parentheses! Yes, parentheses have higher priority than all other arithmetic operators. So the output of the following code is 450!

var x = (100 + 50) * 3;

Conclusion: When you use parentheses in arithmetic operations, the expression in the parentheses must be fully calculated before the program goes to the rest of the code.

There is a complete list of all these preferences that you can access on the operator preference page of the Mozilla network.

Operator Precedence

In this table, the higher the numerical value of the precedence column means, that operator has a higher priority and is calculated earlier than the others. If several operators are placed in the same priority group, the first operator that is on the left will be executed.