What Are JavaScript Math Expressions?

What Are JavaScript Math Expressions? 

What Are JavaScript Math Expressions?

Introduction

JavaScript is a programming language that follows to the ECMAScript specification. It is high-level, just-in-time compiled and multi-paradigm language. JavaScript has dynamic typing, curly-bracket syntax, prototype-based object-orientation, and first-class functions. It is one of the core technologies of the World Wide Web. The huge mainstream of websites uses it for client-side page behavior. Mathematics is an important part of life that we can’t get very far without. This is particularly true when we are learning to program JavaScript. We do depend on processing numerical data, calculating new values, and so on, that we won’t be surprised to learn that JavaScript has a full-featured set of math functions available.

Familiar Operators

We can use a math expression wherever we may use a number. For instance, we’re familiar with this kind of statement.

var popularNumber = 4; But we can also write this. var popularNumber = 2 + 2; we can also write:             alert (2 + 2);

This displays the message “4” in an alert box.

JavaScript always does the math and delivers the result when it sees a math expression. Here’s a statement that subtracts 24 from 12, assigning -12 to the variable.

var popularNumber = 12 – 24;

This one assigns the product to the variable of 3 times 12, 36.

var popularNumber = 3 * 12;

In this one, the number 10 is assigned to a variable. Then 1 is added to the variable, and the sum, 210, is assigned to a second variable.

We can mix variables and numbers as usual.

1 var num = 10;

2 var popularNumber = num + 200;

We can also use nothing but variables.

1 var num = 10;

2 var anotherNum = 1;

3 var popularNumber = num + anotherNum;

The arithmetic operators we’ve been using, +, -, *, and /, are undoubtedly familiar to us.

This one may not be:

var whatsLeftOver = 10 % 3;

% is the modulus operator. It doesn’t give us the result of dividing one number by another. It gives us the remainder when the division is executed. The modulus operation returns 0, if one number divides consistently into another. In the following statement, 0 is assigned to the variable.

var whatsLeftOver = 9 % 3;

Useful Number Method

We’ll use in our JavaScript the Number object. These are an instance of which represents all standard numbers. There is number of useful methods available on it for us to manipulate numbers. Once we’ve read through this module a couple of times it is worth going to the object reference pages and learning more about what’s available.

For example, use the toFixed() method to round the number to a fixed number of decimal places. Type the following lines into browser’s console:

let lotsOfDecimal = 1.766584958675746364;

lotsOfDecimal;

let twoDecimalPlaces = lotsOfDecimal.toFixed(2);

twoDecimalPlaces;

Un-Familiar Operators

There are some specialized math expressions we need to know. Here’s the first one.

num++;

This is a short way of writing…

num = num + 1;

It increments the variable by 1.We decrement using minuses instead of pluses.

num–;

We can use these expressions in an assignment. But the result may surprise us.

1 var num = 1;

2 var newNum = num++;

The original value of num is assigned to newNum in the example above, and num is

incremented afterward. The second statement boosts its value to 2 if num is originally assigned 1 in the first statement. newNum gets the original value of num, 1. We get a different result if we place the pluses before the variable.

1 var num = 1;

2 var newNum = ++num;

Both num and newNum wind up with a value of 2 in the statements above. The new variable is assigned the original value if we put the minuses after the variable,and the first variable is decremented.

1 var num = 1;

2 var newNum = num–;

num is decremented to 0, and newNum gets the original value of num, 1.But if we put the minuses before the variable, newNum is assigned the decremented, not the original, value. Together num and newNum wind up with a value of 0.

1 var num = 1;

2 var newNum = –num;

Complex Arithmetic Expression

One that we may remember from high school algebra, complex arithmetic expressions can pose a problem. What the value of totalCost is in below example.

var totalCost = 1 + 3 * 4;

Depending on the order in which we do the arithmetic the value of totalCost varies. If

we begin by adding 1 + 3, and then multiply the sum by 4, totalCost has the value of 16. But if we go the other way and start by multiplying 3 by 4, then adds 1 to the product, we get 13.

The ambiguity in JavaScript as in algebra is cleared up by precedence rules. The rule as in algebra, that applies here is that multiplication operations are completed before addition operations. So totalCost has the value of 13.

We don’t have to memorize JavaScript’s complex precedence rules. We may tact

the issue by using parentheses to eliminate ambiguity. Parentheses supersede all the built-in precedence rules. They force JavaScript to complete operations enclosed by parentheses before completing any other operations. It also makes our code easier to grasp, both for other coders and for us when we’re trying to understand our own code a year down the road, when we use parentheses to make our intentions clear to JavaScript. The parentheses tell JavaScript to first multiply 3 by 4, and then add 1. The result: 13 in this statement.

var totalCost = 1 + (3 * 4);

The arithmetic is done in a different order if we move the parentheses. The placement of the parentheses tells JavaScript to first add 1 and 3 then multiply by 4 in this next statement. The result is 16.

var totalCost = (1 + 3) * 4;

Here’s another example.

var resultOfComputation = (2 * 4) * 4 + 2; we’ve told JavaScript to do that operation first by placing the first multiplication operation inside parentheses. But then what? The order could be,

  1. Multiply 2 by 4.
  2. Multiply that product by 4.
  3. Giving resultOfComputation values of 34 add 2 to it.Or the order could be…
  4. Multiply 2 by 4.
  5. Multiply that product by the sum of 4 and 2….giving resultOfComputation a value of 48.The solution is more parentheses. If we want the second multiplication to be done before the 2 is added, write this… resultOfComputation = ((2 * 4) * 4) + 2;

But if we want the product of 2 times 4 to be multiplied by the number we get when we total 4 and 2, write this… resultOfComputation = (2 * 4) * (4 + 2);

 

 

Leave a Comment