Approx. read time: 3.6 min.
Post: Introduction to JavaScript – Libraries
Lesson: Introduction to JavaScript Libraries and Instance Methods
Lesson Overview:
In this lesson, you’ll learn the difference between instance methods and library methods in JavaScript, with a particular focus on the Math library. We’ll explore how to use instance-independent methods (also known as static methods) from libraries like Math to perform common tasks such as generating random numbers. You’ll also see examples of how to apply these concepts in JavaScript code.
Objectives:
By the end of this lesson, you will be able to:
- Understand the difference between instance methods and library methods.
- Use static methods from JavaScript libraries without creating an instance.
- Apply Math library methods to generate random numbers.
- Use Math.floor() to round down numbers to the nearest integer.
Key Concepts:
1. Instance Methods vs. Library Methods
- Instance Methods: These are methods that you call on an instance of a class or object. To use these methods, you must first create an instance.
- Library Methods (Static Methods): These are methods you can call directly from a library without needing to create an instance. The Math library is a good example.
2. The Math Library in JavaScript
JavaScript has a built-in Math
library that contains several useful methods. You don’t need to create an instance of Math
to use its methods.
3. Calling Library Methods
To call a method from the Math
library, use the syntax:
For example, the Math.random()
method generates a random number between 0 and 1.
4. Generating Random Numbers with Math.random()
The Math.random()
method returns a decimal number (float) between 0 and 1. To use it, simply write:
5. Generating Random Numbers in a Specific Range
To generate a random number between 0 and a higher number (e.g., 50), you multiply the result of Math.random()
by that number. Example:
The result is still a decimal number.
6. Rounding Down Numbers with Math.floor()
If you want to get a whole number, use Math.floor()
. This method rounds a decimal number down to the nearest integer:
Examples:
Example 1: Calling a Library Method
In this example, we use the Math.random()
method to generate a random number between 0 and 1:
Example 2: Generating a Random Number Between 0 and 50
Here, we multiply the result of Math.random()
by 50 to get a number between 0 and 50:
Example 3: Rounding Down to the Nearest Whole Number
We use Math.floor()
to round down the result to the nearest whole number:
Assignment:
Task 1: Generate a Random Number Between 0 and 100
- Write a script that generates a random whole number between 0 and 100.
- Print the result to the console.
Task 2: Generate a Random Number Between 10 and 100
- Write a script that generates a random whole number between 10 and 100.
- Print the result to the console.
Task 3: Random Dice Roll
- Write a script that simulates the roll of a six-sided dice.
- The result should be a random whole number between 1 and 6.
- Print the result to the console.
Assignment Answer Key:
Task 1: Generate a Random Number Between 0 and 100
Here, we use Math.random()
and multiply it by 100, then round it down with Math.floor()
to get a whole number.
Task 2: Generate a Random Number Between 10 and 100
In this case, we generate a number between 0 and 90 using Math.random()
and multiply by 90. Then we add 10 to shift the range from 10 to 100.
Task 3: Random Dice Roll
To simulate a dice roll, we generate a random number between 0 and 5, then add 1 to shift the result to the range of 1 to 6.
Recap and Next Steps:
- Recap: You now know how to differentiate between instance methods and library (static) methods. You’ve also learned how to use the Math library in JavaScript to generate random numbers, and how to round them to the nearest whole number using
Math.floor()
. - Next Steps: Try experimenting with other methods from the Math library, such as
Math.ceil()
, which rounds up to the nearest integer, andMath.round()
, which rounds to the nearest integer.
Suggested Further Learning:
- Explore other JavaScript libraries like
Date
,String
, orArray
. - Learn more about how static methods differ from instance methods in classes by studying JavaScript’s class system.
Assignment Queries: a. Add unit tests to ensure random number generation behaves as expected for various ranges.
b. Experiment with other Math methods like Math.ceil()
and Math.round()
.
Related Videos:
Related Posts:
Building a web-based JavaScript game