Math Javascript

| | | | | | | | | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Computers are really good at math. It’s in their metaphorical blood, given that computers are powered by the 1’s and 0’s that we call binary. When you are programming, there will probably be a point where you want to do some math.

In JavaScript, math always comes out. You can use math to calculate the size of a window. You can use it to calculate if a user is old enough to use your site. You can use math to add two user-supplied numbers.

In this guide, we will explain how to perform mathematical operations in JavaScript using the five main mathematical operators.

Math operators: an update

JavaScript is made up of a number of operators. Some operators allow you to work with strings, while others help you perform JS math functions. In this guide, we are going to focus on a special type of operator called an arithmetic operator.

Arithmetic operators are symbols used to perform mathematical operations. These JS operators are somewhat similar to the ones you learned in school. For this article, we’ll focus on six operators:

  • Addition (+)
  • Subtraction (-)
  • Division (/ )
  • Multiplication (*)
  • Modulus (%)
  • Power (**)

Beginning of face and learn more about each of them.

Before you begin, it should be noted that all JavaScript numbers are stored as numbers. There is no separate data type for floating point (decimal) numbers or integers. We just call them all "numbers".

Add and Subtract

The JavaScript symbols for adding and subtracting JS numbers are the same ones we use in our everyday math life. How convenient! We can add the numbers indicating the numbers we want to add, separated by a plus sign:

Our code returns: 14. We could do a subtraction by substituting our advantage for a minus:

Our code returns: -4.

JavaScript math can work with both positive and negative numbers.

When working with a mathematical sum, it is probably not enough to print it to the console. variables come into play. You can assign numbers in your math problems to store all the numbers you’re working with:

Our code returns: 30. We assigned the value 10 to a and the value 20 to b Then we used the variable c to add these two numbers. It’s super easy!

Multiply and divide

Let’s go up one level and talk about multiplication and division . Unlike the previous examples, we will have to learn two new operators:

Let’s say we own a cookie factory and we want to figure out how many kilograms of flour we need. Each batch of cookies requires 2 kg of flour and we want to bake five batches. We could calculate how many kilograms of flour we need using this code:

Our code returns: You need 10kg of flour.

You can use the slash to perform split operations.

Let’s say each batch contains 40 cookies and you want to split them into packets. Each pack contains 5 cookies. Now you want to know how many packs you could make. This can be done using the following code:

Our code returns: You can create 8 cookie packages.


The module operator may not be used as widely as the others, but it is still an important part of JavaScript. The modulo operator calculates the remainder of a number after it has been divided.

In JavaScript, the modulo operation is represented using a percent sign. Our last example showed us dividing numbers that can be divided equally. As you know, not all math problems are not run that way ; some return the leftovers.

Going back to our cookie example earlier. Suppose we overestimated the number of cookies we can make in each batch. turns out that each batch will only produce 37 cookies To find out how many packs we can make from our batch we could use this code:

The problem is that we have a deci wrong number ! This is where the module comes in. We could update our t code or calculate the rest of the available cookies:

Our code returns:

We have made some changes to our code. First, we’ve rounded the packsMade value to the nearest whole number. We have used Math.floor () for this purpose. This allows us to see that we can create 7 complete cookie packages.

We therefore used the modulo operator to calculate the remaining cookies that would be left. This tells us that after splitting our packets, there will be two cookies left.


Increase x to the power y. It sounds pretty complicated, especially when you start to use words like exponential. But it doesn’t have to be difficult.

In JavaScript, two asterisks (**) represent the elevation of a number to a power:

This code returns: 343.

This is another way of writing 7 * 7 * 7, but it’s shorter and easier to read.

Order of operations

Mathematics has a specific order of operations. This describes the operations you must perform in which order when faced with a math problem.

One of the most common ways to remember this order is BODMAS:

This tells us that we need to do everything in parentheses first, then all the power calculations, then the division and so on. Consider the following problem:

The answer to this problem is: 17. JavaScript first calculates 5 * 3 because it is in parentheses. It then adds two to the result of this calculation.


JavaScript provides a range of arithmetic operators that you can use to perform mathematical operations. Some of these operators, such as addition, are the same ones we would use to evaluate a math problem; others have their own symbols.

Remember that when evaluating a math problem, you need to consider BODMAS, the order of operations in which the problem will be read.

You are now ready to start doing JavaScript math like a pro!

👻 Read also: what is the best laptop for engineering students?

Math Javascript __del__: Questions


How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.


Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).


Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

Math Javascript __del__: Questions


How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?


Answer #1

Path objects from the Python 3.4+ pathlib module also expose these instance methods:

We hope this article has helped you to resolve the problem. Apart from Math Javascript, check other __del__-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Olivia Lehnman

Shanghai | 2023-01-27

Simply put and clear. Thank you for sharing. Math Javascript and other issues with ast Python module was always my weak point 😁. Will use it in my bachelor thesis

Marie Krasiko

Paris | 2023-01-27

Maybe there are another answers? What Math Javascript exactly means?. I am just not quite sure it is the best method

Carlo Williams

Boston | 2023-01-27

Maybe there are another answers? What Math Javascript exactly means?. Checked yesterday, it works!


Gifts for programmers

Learn programming in R: courses

Gifts for programmers

Best Python online courses for 2022

Gifts for programmers

Best laptop for Fortnite

Gifts for programmers

Best laptop for Excel

Gifts for programmers

Best laptop for Solidworks

Gifts for programmers

Best laptop for Roblox

Gifts for programmers

Best computer for crypto mining

Gifts for programmers

Best laptop for Sims 4


Latest questions


Common xlabel/ylabel for matplotlib subplots

1947 answers


Check if one list is a subset of another in Python

1173 answers


How to specify multiple return types using type-hints

1002 answers


Printing words vertically in Python

909 answers


Python Extract words from a given string

798 answers


Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers


Python os.path.join () method

384 answers


Flake8: Ignore specific warning for entire file

360 answers



Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically