Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In addition to the arithmetic operators you learned, JavaScript has a built-in `Math` object used to perform complex mathematical operations on numbers.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
In this stage, you're going to learn how
to create random numbers in JavaScript.
0:04
Applications often need a way to provide
users random choices or outcomes.
0:08
Generating random numbers
comes in handy for
0:13
games or anytime you want to make
your program less predictable.
0:15
For example, in a dice or
card game, each dice roll or
0:19
card shuffle might result in a different
outcome that's randomly generated.
0:22
You create random numbers in JavaScript
using what's called the math object.
0:26
Even though you've already
worked with objects,
0:30
we haven't specifically talked
about what they are just yet.
0:32
So let me teach you some of
the basics of objects first.
0:36
Then we'll dive into the math object and
start generating randomness.
0:38
In JavaScript, data types like strings,
Booleans, and numbers are formally called
0:42
primitive data types, because they're
basic values built into the language.
0:48
A string or number primitive, by itself,
cannot be altered or manipulated.
0:52
It turns out that JavaScript
automatically adds a special wrapper
0:57
around most primitive types so
that you're able to alter them.
1:01
That wrapper is called an object.
1:05
And as you'll learn, JavaScript is
made up of different types of objects.
1:07
In a previous course,
1:11
you learned that even though a string
is just a series of characters inside
1:12
quotation marks, the JavaScript engine
treats it as an object behind the scenes,
1:16
which unlocks useful functionality
by way of properties and methods.
1:21
All objects have properties.
1:25
A property is just like a variable that's
associated with or attached to the object.
1:27
The string object,
1:32
for example, has a property named length,
which holds a numeric value.
1:33
In fact, you've already used the length
property to find the number of characters
1:37
in a string.
1:40
Each defined string has
its own length property,
1:42
because different strings can have
different number of characters in them.
1:45
Objects also have actions
that they can perform.
1:49
We call these actions methods.
1:52
A string, for example,
has the toUpperCase method,
1:54
which you've used before to convert
a string to all uppercase letters.
1:57
There are other types of
objects in JavaScript,
2:01
which you'll learn as you progress
through our JavaScript curriculum.
2:04
For now, I'll introduce you to a special
object called the Math object.
2:07
In addition to the arithmetic operators
you learned, JavaScript has a built-in
2:12
Math object used to perform complex
mathematical operations on numbers.
2:16
For example,
finding the square root of a number or
2:20
working with trigonometric
functions like tangents, sines and
2:23
cosines, which can help you create
more realistic games and animations.
2:26
The MDN or Mozilla Developer Network
provides useful information
2:31
on the properties and
methods of the Math object.
2:35
Here you can see that the Math object
has some properties and many methods.
2:38
The properties are built-in numbers
you can use in certain calculations.
2:43
For example, PI or Math.PI is used
to calculate the area of a circle.
2:46
The methods are mathematical functions
that are built into JavaScript.
2:53
For example, the Math.round
method takes a number like 2.2,
2:57
and rounds it to the nearest whole number,
2 in this case.
3:02
I'll use the JavaScript
console to demonstrate how
3:06
the Math.round method works.
3:09
The math object is a bit unusual in
that you type the word Math with
3:11
a capital M followed by a period and
the method name.
3:16
You will then provide the method a number
value to work with, like 2.2, and
3:20
the method returns a new value,
in this case, 2.
3:24
I'll try another number,
44.9 and see what happens.
3:28
Math.round rounds 44.9 to the nearest
integer and it returns 45.
3:32
The math object is like a built-in library
full of useful mathematical functions.
3:39
As you can see,
there are lots of different methods for
3:44
specific types of math.
3:47
Some are more complex than others.
3:48
For example,
3:50
Math.max returns the largest number from
a set of number values passed into it.
3:52
And Math.acos calculates
the arccosine of a number.
3:57
I have no idea what that is.
4:02
Up next,
4:04
you'll learn how to create a random number
using JavaScript, a really handy skill.
4:05
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up