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:

  1. Understand the difference between instance methods and library methods.
  2. Use static methods from JavaScript libraries without creating an instance.
  3. Apply Math library methods to generate random numbers.
  4. 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:

Copy to Clipboard

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:

Copy to Clipboard

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:

Copy to Clipboard

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:

Copy to Clipboard

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:

Copy to Clipboard

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:

Copy to Clipboard

Example 3: Rounding Down to the Nearest Whole Number

We use Math.floor() to round down the result to the nearest whole number:

Copy to Clipboard

Assignment:

Task 1: Generate a Random Number Between 0 and 100

  1. Write a script that generates a random whole number between 0 and 100.
  2. Print the result to the console.

Task 2: Generate a Random Number Between 10 and 100

  1. Write a script that generates a random whole number between 10 and 100.
  2. Print the result to the console.

Task 3: Random Dice Roll

  1. Write a script that simulates the roll of a six-sided dice.
  2. The result should be a random whole number between 1 and 6.
  3. 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.

Copy to Clipboard

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.

Copy to Clipboard

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.

Copy to Clipboard

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, and Math.round(), which rounds to the nearest integer.

Suggested Further Learning:

  1. Explore other JavaScript libraries like Date, String, or Array.
  2. 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().

About the Author: Bernard Aybout (Virii8)

I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀