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
In addition to the arithmetic operators you learned, JavaScript has a builtin `Math` object used to perform complex mathematical operations on numbers.
Resources

0:00
[MUSIC]

0:04
In this stage, you're going to learn how to create random numbers in JavaScript.

0:08
Applications often need a way to provide users random choices or outcomes.

0:13
Generating random numbers comes in handy for

0:15
games or anytime you want to make your program less predictable.

0:19
For example, in a dice or card game, each dice roll or

0:22
card shuffle might result in a different outcome that's randomly generated.

0:26
You create random numbers in JavaScript using what's called the math object.

0:30
Even though you've already worked with objects,

0:32
we haven't specifically talked about what they are just yet.

0:36
So let me teach you some of the basics of objects first.

0:38
Then we'll dive into the math object and start generating randomness.

0:42
In JavaScript, data types like strings, Booleans, and numbers are formally called

0:48
primitive data types, because they're basic values built into the language.

0:52
A string or number primitive, by itself, cannot be altered or manipulated.

0:57
It turns out that JavaScript automatically adds a special wrapper

1:01
around most primitive types so that you're able to alter them.

1:05
That wrapper is called an object.

1:07
And as you'll learn, JavaScript is made up of different types of objects.

1:11
In a previous course,

1:12
you learned that even though a string is just a series of characters inside

1:16
quotation marks, the JavaScript engine treats it as an object behind the scenes,

1:21
which unlocks useful functionality by way of properties and methods.

1:25
All objects have properties.

1:27
A property is just like a variable that's associated with or attached to the object.

1:32
The string object,

1:33
for example, has a property named length, which holds a numeric value.

1:37
In fact, you've already used the length property to find the number of characters

1:40
in a string.

1:42
Each defined string has its own link property,

1:45
because different strings can have different number of characters in them.

1:49
Objects also have actions that they can perform.

1:52
We call these actions methods.

1:54
A string, for example, has the toUppercase method,

1:57
which you've used before to convert a string to all uppercase letters.

2:01
There are other types of objects in JavaScript,

2:04
which you'll learn as you progress through our JavaScript curriculum.

2:07
For now, I'll introduce you to a special object called the math object.

2:12
In addition to the arithmetic operators you learned, JavaScript has a builtin

2:16
math object used to perform complex mathematical operations on numbers.

2:20
For example, finding the square root of a number or

2:23
working with trigonometric functions like tangents, sines and

2:26
cosines, which can help you create more realistic games and animations.

2:31
The MDN or Mozilla Developer Network provides useful information

2:35
on the properties and methods of the math object.

2:38
Here you can see that the math object has some properties and many methods.

2:43
The properties are builtin numbers you can use in certain calculations.

2:46
For example, PI or Math.PI is used to calculate the area of a circle.

2:53
The methods are mathematical functions that are built into JavaScript.

2:57
For example, the Math.round method takes a number like 2.2,

3:02
and rounds it to the nearest whole number, 2 in this case.

3:06
I'll use the JavaScript console to demonstrate how

3:09
the Math.round method works.

3:11
The math object is a bit unusual in that you type the word Math with

3:16
a capital M followed by a period and the method name.

3:20
You will then provide the method a number value to work with, like 2.2, and

3:24
the method returns a new value, in this case, 2.

3:28
I'll try another number, 44.9 and see what happens.

3:32
Math.round rounds 44.9 to the nearest integer and it returns 45.

3:39
The math object is like a builtin library full of useful mathematical functions.

3:44
As you can see, there are lots of different methods for

3:47
specific types of math.

3:48
Some are more complex than others.

3:50
For example,

3:52
Math.max returns the largest number from a set of number values passed into it.

3:57
And Math.acos calculates the arccosine of a number.

4:02
I have no idea what that is.

4:04
Up next,

4:05
you'll learn how to create a random number using JavaScript, a really handy skill.
You need to sign up for Treehouse in order to download course files.
Sign up