Main content
Computer programming - JavaScript and the web
Course: Computer programming - JavaScript and the web > Unit 1
Lesson 8: Bonus: Resizing with variablesUsing 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?(830 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(1481 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.(437 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 :)(73 votes)
- What is the difference between / and %?(54 votes)
- x/y returns x divided by y
x%y returns the remainder of x divided by y(102 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.(42 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! :)(141 votes)
- How would the computer evaluate exponents? Is it possible to insert exponents in a code?(13 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 thepow(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!(28 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.(19 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.(12 votes)
- my besti is deaf/hard of hearing can she still code?(10 votes)
- I think so. There is a Transcript tab on every video lesson.(5 votes)
- I feel that you should be notified when people respond to your comment(similar to G-Mail). This is because the response is not instant, and it sometimes takes weeks for someone to scroll down and view your comment. If someone was genuinely stuck in coding, and did not receive feedback, they might stop programming altogether due to negative feelings with the activity.(8 votes)
- Generally notifications show up next to your name in the top left as a blue circle.(5 votes)
- How can i code Javascipt in my laptop, By any tool or software?(7 votes)
- I like to use an application called Visual Studio Code. It also works for other programming languages as well.(1 vote)