Introduction to JavaScript – Libraries

You are here: Home » Blog » Computer Programming Languages » JavaScript » Course 1 - Intro To JavaScript » Introduction to JavaScript – Libraries

Introduction to JavaScript – Libraries

Introduction to JavaScript – Libraries – Instance methods, by definition, require that you create an instance before you can use them. What if you want to call a method without an instance? That’s where JavaScript libraries come in. Libraries contain methods that you can call without creating an instance.

One such collection contains mathematical methods, aptly named the Math library.

Let’s see how you call the .random() method from the Math library:

console.log(Math.random()); // random number between 0 and 1

In the example above, we called the .random() method by appending the library name with a period, the name of the method, and opening (() and closing ()) parentheses. This method returns a random number between 0 and 1. This code prints a random number between 0 and 1.

To generate a random number between 0 and 50, we could multiply this result by 50, like so:

Math.random() * 50;

The answer in the example above will most likely be a decimal. To ensure an answer is a whole number, JavaScript provides a built-in method called Math.floor(). Math.floor() takes a decimal number, and rounds down to the nearest whole number. You can use Math.floor() to round a random number like this:

Math.floor(Math.random() * 50);

In this case:

  1. Math.random generates a random number between 0 and 1.
  2. We then multiply that number by 50, so now we have a number between 0 and 50.
  3. Then, Math.floor rounds the number down to the nearest whole number.

Play with the code below to continue practising your coding skills with math.random and math.floor. Try to make your program generate a random number from 0 to 99.


 

Related Videos:

MiltonMarketing.com Related Videos.

Related Links:

See the JavaScript Glossary on Math Library

Online JavaScript Compiler. Code on the go.

JavaScript Glossary

Systematic approach to Problem Solving

Methods of teaching programming

Alice Teaches OOP (Glossary of useful terms)

Introduction to JavaScript – Control Flow: True and False values

My little pony learning game in VB.NET

Introduction to JavaScript – Review Types and Operators

Introduction to JavaScript – Create a Variable: const

Introduction to JavaScript – Create a Variable: let

Who is this Android App Development course for?

Hello World Android app built with Android Studio

Introduction to JavaScript – Control Flow: if/else Statements

Android Studios

Introduction to JavaScript – Variables: Undefined

Why do most sites use cookies?

About the Author:

I am a loving father, & husband. I am a computer enthusiast. I have used and enjoyed computers since I was young and I enjoy teaching young minds how to code, because it teaches them how to think. Today with YouTube, and social media garbage our youth are losing the ability to think on their own and solve problems. I believe this is a serious epidemic as kids today dont understand that technology is a tool. This tool is being abused, and its underlying effects are taking its toll on kids behaviour, and learning.