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
JavaScript includes a special "object" called the "Math Object" which lets you perform advanced mathematical operations like creating random numbers and performing trigonometric functions.
The Math Object

0:00
In the next video you're going to learn how to create random numbers

0:03
using JavaScript.

0:05
Generating random numbers comes in handy for games or

0:08
anytime you want to make your program less predictable.

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

0:16
Let me teach you about the math object first and then we'll jump into randomness.

0:20
JavaScript is made up of different types of objects.

0:24
Numbers are one type of object and strings are another type of object.

0:28
A string as you learned in the last stage is a bunch of

0:30
characters inside quote marks.

0:33
Objects have properties.

0:35
A property is just like a variable that's associated with the object.

0:38
A string object for example has a property named length.

0:42
The length property holds a numeric value, the number of characters in the string.

0:48
Each string has its own length property because different strings can

0:51
have different numbers of characters in them.

0:55
Objects also have actions that they can perform, we call these actions methods.

1:00
The strings for example have the toUpperCase method

1:03
which takes the string and returns another string with all uppercase letters.

1:08
Strings are just one type of object in JavaScript.

1:11
There are many others, which we'll look at in this course.

1:14
But for now, let's talk about a special object called the math object.

1:18
The math object is used to perform complex mathematical functions, like finding

1:23
the square root of a number or working with trigonometric functions like tangent,

1:28
sines, and cosines to create more realistic games and animations.

1:33
The MDN or Mozilla Developer Network provides great information on

1:37
the properties and methods of the math object.

1:41
As you can see the math object has some properties and some methods.

1:45
The properties are numbers that are used in certain calculations.

1:48
For example, pi is used to calculate the area of a circle.

1:52
The methods are mathematical functions that JavaScript is programmed to perform.

1:57
For example, the math.round method will take a number like 2.2 and

2:02
round it to the nearest whole number, two in this case.

2:06
Let's see how this works.

2:08
Remember, you can run lines of JavaScript code in a browser's JavaScript console.

2:12
[BLANK_AUDIO]

2:15
[LAUGH] Look, Mozilla has hid a little ASCII art in the console for us.

2:20
When the console gets too full of information, like it is here, you can

2:23
type clear, CLEAR, with parentheses, to erase what's in the console.

2:29
We can use the console to test a short snippet of code,

2:32
and we can see how the round method works.

2:36
Now the math object is a bit unusual.

2:38
You type the word math with a capital M, followed by a period and the method name.

2:44
You can then provide a value for

2:45
that method to work with and the method returns a new value, two in this case.

2:51
Here I'll try another number, 44.9, let's see what happens.

2:55
There are a lot of different methods for

2:57
specific types of math, like calculating the arc cosine of a number.

3:01
I have no idea what that is.

3:03
Or the tangent of a value.

3:06
Some of these methods, you should avoid.

3:07
Notice this little beaker icon.

3:09
This represents an experimental method.

3:12
It's a method that's not supported by all browsers, and may not work if you try it.

3:16
The methods that don't have that icon are generally well supported and

3:19
you should feel free to try them out.

3:22
In the next video,

3:23
you'll learn how to create a random number using JavaScript, a very useful skill.
You need to sign up for Treehouse in order to download course files.
Sign up