Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Number 3/3 | Data Types and Variables
Introduction to JavaScript

book
Number 3/3

Other Math Functions

JavaScript has a library by the name of Math that has many built-in mathematics functions and methods. Let’s look at a few of them.

  • Math.random():
    The Math.random() is a function in JavaScript, that gives random numbers between 0 and 1 but not including 1, for example.

console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
123
console.log(Math.random()); console.log(Math.random()); console.log(Math.random());
copy

As you can see we get three different numbers between 0 and 1.

Note:
You may get different output as Math.random() gives random numbers.

  • Math.max() and Math.min():
    The Math.max() returns the greatest number of all the numbers passed in as an argument while Math.min() returns the smallest one.

console.log(Math.max(3, 5, -10, 100, 1));
console.log(Math.min(1, 2, 6, 0, -15));
12
console.log(Math.max(3, 5, -10, 100, 1)); console.log(Math.min(1, 2, 6, 0, -15));
copy
  • Math.pow(base, power):
    The Math.pow(base, power) returns base raised to the given power, for example.

console.log(Math.pow(2, 10));
1
console.log(Math.pow(2, 10));
copy
Compito

Swipe to start coding

The task is to find the volume of a cylinder. The radius and height variables are given to storing the radius and the height of the cylinder. The formula for the volume of the cylinder is:
V = πhr²
where π is the Math PI having the value of 3.1415, r is the radius, and h is the height. Your task is to create a new const variable named PI and store 3.1415 in it and implement this formula storing the result in a variable named volume.

Soluzione

let radius = 5;
let height = 3;
let radiusSquared = Math.pow(radius, 2);
const PI = 3.1415;
volume = PI * radiusSquared * height;
console.log(volume);

Summary

  • JavaScript provides the facility of rounding numbers.

  • We can check whether a variable is finite or infinite through the isFinite() function.

  • We can check whether a variable is a number or not through the isNaN() function.

  • The Math.random() function is used to get a random numeric value from 0 to 1(excluding 1).

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 14
let radius = 5;
let height = 3;

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

We use cookies to make your experience better!
some-alt