Skip to main content

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);

 

 

Popular posts from this blog

What is Internet of Things (IoT)?

 What is Internet of Things (IoT)?    The internet of Things, or IoT, refers to the billions of physical devices  round the  world that are now connected to  the web  , collecting, sharing and analyses  of knowledge  . It presents and provides the explanation of the network of physical things which is embedded with software , sensors,  and other technologies for the benefit of connecting and exchanging data with other devices and systems on the internet   . Due to the cheap processors and wireless networks, it's possible to  means  anything, from a pill to an aero plane to a self-driving car into  an area  of the IoT. Overview IoT will control the Fourth  technological revolution  . The Fourth  technological revolution  is changing the very software-defined automation allows manufacturers to link all stages of  the worth  chain, rapidly adapt to changing markets , and make highly personalized products on a mass scale. The opportunities provided by this revolution are authenticated

What is Rust Borrowing?

What is Rust Borrowing? Borrowing We call having references as function parameters borrowing. As in real world , if an individual owns something, we 'll borrow it from them . Whenever we’re done, we 've got to offer it back. Referencing  The reference is that the act of consulting somebody or something so as to urge information We would  declare reference in terms of Rust programing as if we are taking a replica of it without damaging or taking its ownership. ‘&’ symbol is employed to pass the reference Have a look that  how ‘&’ symbol is employed in code. fn main() { let s1 = String::from("hello"); let len = calculate_length(&s1); println!("The length of {} is {}.", s1, len); } fn calculate_length(s: &String) -> usize { s.len()} Mutable References The actual concept of mutable reference is same as we use mutable variable When  we  need to  modify  mutable reference is employed once   the worth we make regard to.

Kubernetes Readiness Probes

 Kubernetes Readiness Probes Description We understand that liveness probes and the way they assist keep our apps healthy by ensuring unhealthy containers are restarted automatically. Same like liveness probes, Kubernetes permit us to also define a readiness looked for our pod. The readiness probe is deployed periodically and examines whether the precise pod should receive client requests or not. Whenever a container’s readiness probe returns back success, it’s signaling that the container is ready to simply accept requests. This notion of being prepared is clearly something that’s specific to every container. Almost as liveness probe Kubernetes sends requests to container and supported the result either successful or unsuccessful response it decides container is prepared to require traffic or still preparing for that. The liveness probes not like, if a container fails the readiness check, it won’t be killed or restarted.   This is a very good practice to