Course Content
Introduction to JavaScript (staging)
Introduction to JavaScript (staging)
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()
:
TheMath.random()
is a function in JavaScript, that gives random numbers between0
and1
but not including1
, for example.
console.log(Math.random()); console.log(Math.random()); console.log(Math.random());
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()
andMath.min()
:
TheMath.max()
returns the greatest number of all the numbers passed in as an argument whileMath.min()
returns the smallest one.
console.log(Math.max(3, 5, -10, 100, 1)); console.log(Math.min(1, 2, 6, 0, -15));
Math.pow(base, power)
:
TheMath.pow(base, power)
returnsbase
raised to the givenpower
, for example.
console.log(Math.pow(2, 10));
Swipe to show code editor
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
.
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 from0
to1
(excluding1
).
Thanks for your feedback!
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()
:
TheMath.random()
is a function in JavaScript, that gives random numbers between0
and1
but not including1
, for example.
console.log(Math.random()); console.log(Math.random()); console.log(Math.random());
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()
andMath.min()
:
TheMath.max()
returns the greatest number of all the numbers passed in as an argument whileMath.min()
returns the smallest one.
console.log(Math.max(3, 5, -10, 100, 1)); console.log(Math.min(1, 2, 6, 0, -15));
Math.pow(base, power)
:
TheMath.pow(base, power)
returnsbase
raised to the givenpower
, for example.
console.log(Math.pow(2, 10));
Swipe to show code editor
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
.
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 from0
to1
(excluding1
).
Thanks for your feedback!