You've already started watching Creating Numbers: Part 1
There are several ways to define a number in JavaScript. In this video we take a look at the syntax for creating several types of numbers in JavaScript.

0:00
[? Music ?] [Treehouse]

0:08
Numbers are an important part of any program,

0:10
and JavaScript makes it really easy to work with numbers.

0:13
In this video, we'll see how to define our numbers in our JavaScript programs.

0:18
Now we'll be learning about numbers in JavaScript.

0:20
I have the basic HTML and JavaScript page set up that we've been using

0:24
in the previous videos set up right now,

0:26
so we have an index.html file,

0:29
and at the bottom we have a script with the source of numbers.js.

0:34
This numbers.js file is where we'll be experimenting with numbers in JavaScript.

0:39
Now, we've seen the basic usage of numbers in JavaScript before

0:43
while we learned Introduction to Programming and in some of our earlier videos.

0:47
What we're going to do now is take a more indepth look at JavaScript numbers

0:51
and the different ways we can create them and manipulate them.

0:54
Let's take a look at the different ways we can create numbers.

0:57
The basic way of creating a JavaScript number is typing in

1:00
decimal numbers 0 through 9 to represent your number.

1:03
Let's store a number in a variable called "a,"

1:07
and let's just make the number 11.

1:10
This is the simplest way we can create a number in JavaScript.

1:13
We simply type in the number 11, and that number is now stored in our variable.

1:19
We could create a negative number by simply prepending a minus sign

1:24
to the beginning of our number and then typing in

1:27
our digits as usual, so 123 can be represented by 1, 2, 3.

1:33
And if we save it out and go to our page,

1:35
we can refresh it, and we can inspect the different numbers.

1:38
For instance, a was the number 11,

1:40
and b was the number 123.

1:44
That's pretty easy.

1:48
What we see now is an example of whole numbers.

1:54
We could also use the decimal point to create numbers

1:56
of arbitrary precision past the decimal point.

1:59
Let's create variable c,

2:03
and if we wanted to represent the number 1.5, the way we would do that

2:07
is by typing "1.5," and we can add more numbers to that.

2:12
For instance, we can make d = 123.456 or 789.

2:20
If we go back to our page and we check out the numbers c and d,

2:25
we have the numbers 1.5 and 123.456789.

2:31
There is a very important detail when considering

2:34
these floating point numbers versus these whole numbers.

2:38
In some languages, when we represent a whole number

2:41
or represent a number with a decimal place,

2:43
they're actually represented by different types of numbers.

2:47
For instance, in some languages, a whole number would be considered an integer,

2:51
or an int, and a number with a decimal point

2:54
would be a floating point number, meaning it has some arbitrary

2:58
precision past the decimal point.

3:00
But in JavaScript, however, there's only 1 type of number,

3:03
so even though the number 11 looks like it's only a whole number,

3:07
it's really 11.0.

3:10
Now, this has some implications, because in some languages,

3:13
when you, for instance, divide 2 whole numbers or integers,

3:17
the answer, because both of the operands were integers,

3:20
would also be an integer, perhaps rounded down.

3:23
However, in JavaScript, no matter what, since they're all 1 type of number,

3:27
we'll get these floating point type numbers any time we do an operation.

3:33
Because all numbers in JavaScript are floating point,

3:35
there is a specific type of, perhaps, error

3:39
that you would have to watch out for when doing mathematical operations

3:42
on our numbers, so let's take an example.

3:45
We'll create a variable called "e," which is 0.1.

3:49
And we'll create a variable called "f," which is 0.2.

3:52
If we create a variable called "result,"

3:55
and we'll set its value to e * f

3:59
the asterisk here is the multiplication operator, so we're multiplying 0.1 by 0.2.

4:07
Now, we would expect the answer to be 0.02.

4:11
However, if we check it out in the browser

4:14
when we refresh the page and we see what is stored in result,

4:18
we'll see we get something very near to 0.2,

4:21
but it's actually 0.020000 so on and so forth,

4:26
and then a 4 at the end, and this is a rounding error,

4:30
and this is the result of how numbers are stored on the computer.

4:33
Because each number has a very limited amount of space to store

4:37
its representation on the computer, and there's an infinite amount of numbers

4:41
that could be represented using this decimal notation,

4:44
some rounding has to occur, and usually you can see this

4:47
when you multiply numbers like 0.1 and 0.2 together

4:52
where you get a number that's very near the result you want

4:55
but not quite the result.

4:57
Usually this doesn't happen when multiplying or dividing whole numbers

5:00
because the way the number is stored in the computer

5:02
it is better able to represent the results.

5:05
For instance, 2 times 3 would equal 6,

5:10
so you don't really have to worry about whole numbers.

5:13
Now, there are various ways that if this does become a problem in your program

5:16
that you can get around it either by manually rounding your results

5:21
or, for instance, if you're dealing with money, and you use dollars and cents

5:24
by saying $10.50 would be represented by 10.5,

5:29
to avoid these errors, what you could do is represent money, for instance,

5:34
in cents, so instead of $10.50 being represented by 10.5,

5:40
it would be 1,050 cents.

5:43
That way multiplication and division operations wouldn't be affected

5:47
by the fact that your cents are represented to the righthand side

5:50
of the decimal point.

5:52
We've seen how to create whole numbers and floating point numbers in JavaScript

5:57
as well as how to use the negative operator to create negative numbers.

6:01
In the next video, we'll see a couple other ways to create numbers in JavaScript.

6:06
So far we've seen some basic ways of defining numbers in our JavaScript program,

6:10
but there are a few more advanced ways that we can define our numbers.

6:14
In the next video, we'll see what those are.

6:17
[? Music ?] [Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up