Main content

## Computer programming

# Using math expressions in JS

In the JavaScript language (and most programming languages), we can use mathematical operators to calculate numbers and create expressions.

You've already seen examples of adding in JS, using the

`+`

operator. You can also use `-`

for subtraction, `*`

for multiplication, `/`

for division, and `%`

to take the remainder. Here are examples of those in action:```
var x = 10;
var a = x + 5; // add 5, result is 15
var b = x - 5; // subtract 5, result is 5
var c = x * 2; // multiply by 2, result is 20
var d = x / 4; // divide by 4, result is 2.5
var e = x % 3; // divide by 3 & return remainder, result is 1
```

When you use multiple math operators in a single expression, the computer follows an "order of operations" to make sure it computes the result the same way each time. It's the same order of operations that you probably learned in math class, and is commonly remembered as "PEMDAS" - parentheses, exponents, multiplication, division, addition, subtraction. You can learn more about order of operations on Khan Academy.

For example, in the following code, it would first evaluate the two expressions in parentheses, then it would multiply those results together, and finally, it would add 100 to that result.

```
var x = 10;
var a = (x + 10) * (x / 2) + 100;
```

Just like in math expressions, you only need parentheses if you want your expression to be evaluated differently than it would be according to the order of operations. You can leave them out otherwise.

Next up, we'll go through an example using more complicated expressions with variables and math operators. If you want, play around with these math expressions first, to make sure you have a handle on how the computer evaluates them.

## Want to join the conversation?

- What is the question is 4%5? Does it return a 0 or a 5?(821 votes)
- 4 % 5 actually returns 4. It basically divides 4 by 5 and says okay, well that is 0 remainder 4, so it's just 4(1461 votes)

- Not about this topic, but how do I program my animations to stop at a certain point? For example to program my sun to grow until it has filled more than 25% of the screen.(432 votes)
- Let's say you are using the variable shapeWidth and shapeHeight for the size of a shape.

Then you can use something like this inside the draw loop:`if (shapeWidth < width) {`

shapeWidth ++;

}

if (shapeHeight < height) {

shapeHeight ++;

}

this will prevent the variable from getting bigger than 25% of the screen. "width" and "height" are the dimensions of the canvas. I hope this helps :)(67 votes)

- What is the difference between / and %?(53 votes)
- x/y returns x divided by y

x%y returns the remainder of x divided by y(98 votes)

- Hi! I still do not understand what the % is used for. It represents the remainder, but how? I'm not exactly sure and I don't really understand the example used in the article.(39 votes)
- What they mean by remainder is that it is the amount left over when you divide the first value with the second one. For example, if you did 75 % 4, it would mean 75/4 which is 18 with 3 remainder. So 75 % 4 = 3. Hope this helps! :)(136 votes)

- How would the computer evaluate exponents? Is it possible to insert exponents in a code?(12 votes)
- Most computers use a combination of logarithms and some trigonometry to calculate exponents. This method is very complicated, so I'll give you a link at the bottom. I've heard that the Taylor Series is also used to calculate exponents (I don't know if this is better than the first method).

If you want to use exponents here on Khan Academy, then use the`pow(base, exponent)`

function. Be careful, exponents grow very fast and can overload your computer if you don't use them wisely. Here are a few links:

* https://stackoverflow.com/a/165181/6928996

* https://www.khanacademy.org/computer-programming/pownum-exponent/877858853

* https://en.wikipedia.org/wiki/Taylor_series#Exponential_function

Good luck and happy coding!(26 votes)

- I don’ t rly understand how % and / works in coding(8 votes)
`/`

is just division. The computer will return the quotient of the two numbers.`%`

is the modulus operator, which will divide the numbers and return the remainder.(18 votes)

- Do exponents work? I tried them once with no success... any examples or tips?(9 votes)
- Javascript does not have exponentiation. Use the
`pow`

function to raise bases to powers.(11 votes)

- How can i code Javascipt in my laptop, By any tool or software?(7 votes)
- there is a website called"javascript which you can then download.(0 votes)

- I don't really get the whole
`%`

thing. Can someone expound on it in laymen's terms?(7 votes)`%`

is the mod operator. When used in your code it will form an expression for the operand on its left mod the operand on its right.

The mod operator simply returns the remainder when 2 numbers are divided.

For example,`7 % 3 // returns 1 because 3 goes into 7 twice with 1 left over`

(3 votes)

- where should i use javascript to make a game ?(4 votes)
- If you want to start your own Processing Javascript code from scratch, use this link:

https://www.khanacademy.org/computer-programming/new/pjs(7 votes)