1 00:00:00,000 --> 00:00:04,604 [MUSIC] 2 00:00:04,604 --> 00:00:08,920 In this stage, you're going to learn how to create random numbers in JavaScript. 3 00:00:08,920 --> 00:00:13,250 Applications often need a way to provide users random choices or outcomes. 4 00:00:13,250 --> 00:00:15,360 Generating random numbers comes in handy for 5 00:00:15,360 --> 00:00:19,010 games or anytime you want to make your program less predictable. 6 00:00:19,010 --> 00:00:22,310 For example, in a dice or card game, each dice roll or 7 00:00:22,310 --> 00:00:25,460 card shuffle might result in a different outcome that's randomly generated. 8 00:00:26,790 --> 00:00:30,850 You create random numbers in JavaScript using what's called the math object. 9 00:00:30,850 --> 00:00:32,730 Even though you've already worked with objects, 10 00:00:32,730 --> 00:00:36,080 we haven't specifically talked about what they are just yet. 11 00:00:36,080 --> 00:00:38,920 So let me teach you some of the basics of objects first. 12 00:00:38,920 --> 00:00:42,900 Then we'll dive into the math object and start generating randomness. 13 00:00:42,900 --> 00:00:48,150 In JavaScript, data types like strings, Booleans, and numbers are formally called 14 00:00:48,150 --> 00:00:52,750 primitive data types, because they're basic values built into the language. 15 00:00:52,750 --> 00:00:57,920 A string or number primitive, by itself, cannot be altered or manipulated. 16 00:00:57,920 --> 00:01:01,700 It turns out that JavaScript automatically adds a special wrapper 17 00:01:01,700 --> 00:01:05,130 around most primitive types so that you're able to alter them. 18 00:01:05,130 --> 00:01:07,308 That wrapper is called an object. 19 00:01:07,308 --> 00:01:10,430 And as you'll learn, JavaScript is made up of different types of objects. 20 00:01:11,590 --> 00:01:12,740 In a previous course, 21 00:01:12,740 --> 00:01:16,800 you learned that even though a string is just a series of characters inside 22 00:01:16,800 --> 00:01:21,260 quotation marks, the JavaScript engine treats it as an object behind the scenes, 23 00:01:21,260 --> 00:01:24,580 which unlocks useful functionality by way of properties and methods. 24 00:01:25,630 --> 00:01:27,480 All objects have properties. 25 00:01:27,480 --> 00:01:32,220 A property is just like a variable that's associated with or attached to the object. 26 00:01:32,220 --> 00:01:33,110 The string object, 27 00:01:33,110 --> 00:01:37,170 for example, has a property named length, which holds a numeric value. 28 00:01:37,170 --> 00:01:40,940 In fact, you've already used the length property to find the number of characters 29 00:01:40,940 --> 00:01:41,460 in a string. 30 00:01:42,810 --> 00:01:45,420 Each defined string has its own length property, 31 00:01:45,420 --> 00:01:49,460 because different strings can have different number of characters in them. 32 00:01:49,460 --> 00:01:52,340 Objects also have actions that they can perform. 33 00:01:52,340 --> 00:01:54,680 We call these actions methods. 34 00:01:54,680 --> 00:01:57,590 A string, for example, has the toUpperCase method, 35 00:01:57,590 --> 00:02:01,610 which you've used before to convert a string to all uppercase letters. 36 00:02:01,610 --> 00:02:04,240 There are other types of objects in JavaScript, 37 00:02:04,240 --> 00:02:07,190 which you'll learn as you progress through our JavaScript curriculum. 38 00:02:07,190 --> 00:02:10,720 For now, I'll introduce you to a special object called the Math object. 39 00:02:12,280 --> 00:02:16,240 In addition to the arithmetic operators you learned, JavaScript has a built-in 40 00:02:16,240 --> 00:02:20,520 Math object used to perform complex mathematical operations on numbers. 41 00:02:20,520 --> 00:02:23,390 For example, finding the square root of a number or 42 00:02:23,390 --> 00:02:26,640 working with trigonometric functions like tangents, sines and 43 00:02:26,640 --> 00:02:31,450 cosines, which can help you create more realistic games and animations. 44 00:02:31,450 --> 00:02:35,610 The MDN or Mozilla Developer Network provides useful information 45 00:02:35,610 --> 00:02:38,500 on the properties and methods of the Math object. 46 00:02:38,500 --> 00:02:43,300 Here you can see that the Math object has some properties and many methods. 47 00:02:43,300 --> 00:02:46,881 The properties are built-in numbers you can use in certain calculations. 48 00:02:46,881 --> 00:02:51,770 For example, PI or Math.PI is used to calculate the area of a circle. 49 00:02:53,050 --> 00:02:57,334 The methods are mathematical functions that are built into JavaScript. 50 00:02:57,334 --> 00:03:02,185 For example, the Math.round method takes a number like 2.2, 51 00:03:02,185 --> 00:03:06,527 and rounds it to the nearest whole number, 2 in this case. 52 00:03:06,527 --> 00:03:09,580 I'll use the JavaScript console to demonstrate how 53 00:03:09,580 --> 00:03:11,470 the Math.round method works. 54 00:03:11,470 --> 00:03:16,192 The math object is a bit unusual in that you type the word Math with 55 00:03:16,192 --> 00:03:20,045 a capital M followed by a period and the method name. 56 00:03:20,045 --> 00:03:24,867 You will then provide the method a number value to work with, like 2.2, and 57 00:03:24,867 --> 00:03:28,022 the method returns a new value, in this case, 2. 58 00:03:28,022 --> 00:03:32,730 I'll try another number, 44.9 and see what happens. 59 00:03:32,730 --> 00:03:39,560 Math.round rounds 44.9 to the nearest integer and it returns 45. 60 00:03:39,560 --> 00:03:44,960 The math object is like a built-in library full of useful mathematical functions. 61 00:03:44,960 --> 00:03:47,250 As you can see, there are lots of different methods for 62 00:03:47,250 --> 00:03:48,890 specific types of math. 63 00:03:48,890 --> 00:03:50,828 Some are more complex than others. 64 00:03:50,828 --> 00:03:52,121 For example, 65 00:03:52,121 --> 00:03:57,510 Math.max returns the largest number from a set of number values passed into it. 66 00:03:57,510 --> 00:04:02,290 And Math.acos calculates the arccosine of a number. 67 00:04:02,290 --> 00:04:03,680 I have no idea what that is. 68 00:04:04,750 --> 00:04:05,500 Up next, 69 00:04:05,500 --> 00:04:09,940 you'll learn how to create a random number using JavaScript, a really handy skill.