Jump to content
Sign in to follow this  

Javascript Tut - Mathematical Operations

Recommended Posts

You can use the mathematical operators with variables. This allows you to add up, multiply, subtract, and divide. The mathematical operators are these:

  • The plus symbol ( + ) is used for addition
  • The minus symbol ( - ) is used for subtraction
  • The asterisk symbol ( * ) is used for multiplication
  • The forward slash symbol ( / ) is used for division
  • The percentage symbol ( % ) is used for modulus calculations

To get some practice with these symbols, create a new web page. Or you can reuse the one fromthe previous section. Add the two Javascript tags in the HEAD section. Between the two Javascript tags add the following code:

var number1 = 42;
var number2 = 100;
var total;

total = number1 + number2;
document.write( total );

Your code should then look like this:


Save your work and load the page in a browser. You should see the number 142 written to the page. (If you don't see anything, check your code to make sure it looks exactly like ours.)

What we did here is to store the numbers 42 and 100 into two different variable, number1 andnumber2. We then set up a third variable called total. On the fourth line of the code we have this:

total = number1 + number2;

To the right of the equal sign are our two variables, with a plus symbol between the two. The plus symbol this time doesn't mean join together (concatenate). This time the plus symbol means "add up". Javascript doesn't get confused because the contents of both variables are numbers, and it knows it should be adding them up, not concatenating.

As an example of the above, try putting quote marks around the 100:

var number2 = "100";

When you save your changes and refresh the page in the browser, you'll see it writes this:


Putting quotes around the 100 turns it into text. Now that one of the variables is text and the other is a number, Javascript concatenates the two rather than adds them up.

Remove the quote marks from around 100. Change the plus symbol into a minus symbol. Save your changes and refresh the page in your browser. What total do you get now?

Change the minus symbol into the symbol for multiplication, an asterisk symbol (*). Save your change and refresh the page in your browser. What total do you get now?

Change the plus symbol into the symbol for division. Save your change and refresh the page in your browser. What's the new total?

You can use normal numbers with the mathematical operators. In the code below, we're adding up two numbers and storing the result in a variable:

var result;
result = 12 + 24;
document.write( result );

Javascript will add up the numbers on the right of the equal sign. When it has finished adding, it will store the result in the variable on the left of the equal sign.

Storing Javascript Objects in Variables

In the previous section we used an alert message and document.write with the various objects. This kind of thing:

alert ( window.innerWidth );

But you can store these values in variables as well. If you want to store the innerWidth value, for example, you can do it like this:

var width = window.innerWidth;

Once you have the value stored in a variable you can do other things with it later:

alert( width );


This was not created by me. Credits to the authors!

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

About us

MMO-Society is the Forum former known as Lordcraft, MMO-Society offers various of Resources to help you evolve your knowledge within Emulation, In the process we make sure to form a strong community bond in-between each other and thus we are proud to can admit that MMO-Society is possibly the friendliest Community Forum to currently exist.

Useful Quick-Links

  • Create New...