If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

## Computer programming - JavaScript and the web

### Course: Computer programming - JavaScript and the web>Unit 1

Lesson 8: Bonus: Resizing with variables

# 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?
• 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
• 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.
• 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 :)
• What is the difference between / and %?
• x/y returns x divided by y
x%y returns the remainder of x divided by y
• 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.
• 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! :)
• How would the computer evaluate exponents? Is it possible to insert exponents in a code?
• I don’ t rly understand how % and / works in coding
• `/` 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.
• Do exponents work? I tried them once with no success... any examples or tips?
• Javascript does not have exponentiation. Use the `pow` function to raise bases to powers.