Math Operators in JavaScript

  • Videos
  • January 12, 2022
  • 0

Morten Rand-Hendriksen

Topics: JavaScript, Operators

Transcript Summary

Often in time, computers are great in doing math and in JavaScript. We often are required to perform math by converting or combining numbers. As a result, we have a standard arithmetic math operator to help us with this. To use addition, simply use the plus symbols, and vice versa. Consider the following scenario: we have two values. One is A equals 5, while the other is B equals 4. If we say A plus B, we get 5 plus 9, which is 9. The same may be said if you wished to subtract, multiply, divide, or do anything else. We can also do more advanced arithmetic. Advanced arithmetic, for example, the use of parentheses. Let’s say A multiplied by B divided by two. With the values we used previously, A multiplied by B equals 20. And when we divide that number by two, we get ten. Using these standard arithmetic operators, we may perform complex math within JavaScript. 

Having said that, there are a variety of other math operators that should be discussed. We have more advanced operators at our disposal. One example is the percentage symbol, which represents a modulus. Modulus are frequently useful while performing tasks such as testing for prime value or other similar tasks. In fact, it is frequently utilized in mathematics. Exponentiation is another advanced operator. To get the value of a number using exponentiation, we must use asterisks. 

There is one thing in JavaScript that we will frequently come into that may have little relevance outside of standard math. That is, it consists of having a number and then increasing or decreasing the value by one. So, if we want to add one to the number or subtract one from it. There is a specialized formula in JavaScript that works somewhat differently. For instance, if A is 15 and we say plus A. If we save this, the output is 16 since we took A and increased the value by one. Now if we were to replace it with two minus symbols, we get 14 because we removed A and then took another away. In other words, we can control how the value is presented by inserting a symbol before or after it. 

Another thing to keep in mind is that JavaScript frequently interprets a single number within a string as the number itself. For example, suppose we mixed true numbers with string numbers and then used the plus symbol to add them. The plus symbol will be used as a string combiner in this instance, which is problematic. It only happens with the plus sign. In other words, when we conduct math, we must ensure that we are working with real numbers rather than strings, or we may get strange results that make no sense from a mathematical standpoint. 


Let Statement in JavaScript

React JS Font-End Development Tutorial for Beginners