Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn basic mathematical procedures using JavaScript: add, subtract, multiple and divide numbers.

0:00
You'll probably spend a lot of time working with numbers in JavaScript.

0:04
If you create a game, for example, you'll need to keep track of a player's score.

0:08
If you build an ecommerce site,

0:10
you'll need to figure out the total cost of items in a visitor's shopping cart.

0:15
And if you want to calculate the number of days until your next birthday,

0:18
you'll need to use numbers and do some math.

0:22
JavaScript lets you perform most mathematical calculations.

0:25
Including addition, subtraction, multiplication, and division.

0:30
For example, to add two numbers together you use the plus symbol.

0:33
Also called the addition operator like this, two plus seven.

0:39
Notice that we used the same plus symbol that we used for combining two strings.

0:43
As I discussed in an earlier video, in this stage adding a plus between two

0:47
strings creates one longer mushed together string.

0:51
Remember, combining strings is called concatenation.

0:55
But with numbers the plus operator works as you would expect it to,

0:59
it does the math and adds the numbers.

1:02
To subtract numbers use a minus sign, four minus three for example.

1:06
To divide two numbers, you use the slash character, six divided by

1:11
three which means divide the number on the left by the number on the right.

1:15
In this example, divide six by three which is two by the way.

1:18
To multiply use an asterisk.

1:22
This is ten times nine.

1:24
You won't spend much time doing literal calculations with Java Script.

1:28
I mean, you don't need to ask java script what two plus two is.

1:31
We have calculators for that.

1:33
However, you will frequently store numbers in variables and

1:36
then perform calculations with those variables.

1:39
For example, you might start off a game by creating a variable named score.

1:45
And storing the number zero in it.

1:47
Later in the program, maybe when the player blows up an invading spaceship,

1:52
you add 100 points to that score.

1:55
The variable score appears twice in this statement.

1:59
Remember when putting a value into a variable that the stuff on

2:02
the right goes into the variable on the left.

2:05
In this case,

2:06
the stuff on the right is the current contents of the variable score plus 100.

2:10
That total is then stored back into the variable on the left.

2:16
This is how we update a variable.

2:17
[SOUND] There are shorthand methods for

2:20
performing each of the basic mathematical operations on a variable.

2:24
Addition, subtraction, multiplication, and division.

2:28
For example to add ten to the contents of a variable you'd type plus equals ten.

2:33
This both adds ten to the current variable and

2:36
assigns the result back into the same variable.

2:39
There are also short hands for subtracting from a variable,

2:43
multiplying the variable by a value and dividing the variable by another value.

2:50
Let's do some math with JavaScript.

2:52
We will build a simple program that can help us calculate the number of

2:55
seconds in a day, hours in a week, or minutes in a year.

2:59
You can follow along in workspaces,

3:01
just click the Launch Workspace button on this page.

3:03
Open the script.js file.

3:08
Let's start by creating some variables that hold information about time.

3:12
First, let's create a variable that stores the number of seconds in a minute.

3:15
[BLANK_AUDIO]

3:18
Then, let's create a few more variables for other lengths of time.

3:22
[BLANK_AUDIO]

3:28
With this information, we can now start to do some math.

3:32
We're gonna print out a message on the page that lists the number of

3:35
seconds in one day.

3:36
You can start by calculating the number of seconds in a day by multiplying the number

3:41
of seconds in a minute by the number of minutes in an hour,

3:44
by the number of hours in a day.

3:47
Now we know the number of seconds per day, and we can write that out to the document.

3:51
[BLANK_AUDIO]

3:57
I'm gonna save this file and then preview it.

4:03
Looks great.

4:04
So here's a little assignment for you.

4:06
Add another variable named years alive, and assign it your age.

4:12
Then print out a second message to the page,

4:15
that is add another document.write statement.

4:18
The message should say I've been alive for more than x seconds.

4:22
Now replace x with the number of seconds that have elapsed in

4:26
all the years you've been alive.

4:28
You'll need to do some multiplication of variables to get it done.

4:31
Good luck.
You need to sign up for Treehouse in order to download course files.
Sign up