top of page

JavaScript Math, Random and Boolean

Updated: Mar 23, 2021

JavaScript Math Object


The JavaScript Math object allows you to perform mathematical tasks on numbers.



Example:




Math.sin()

  • Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).

  • If you want to use degrees instead of radians, you have to convert degrees to radians:

  • Angle in radians = Angle in degrees x PI / 180.

Example:


Math.min() and Math.max()


Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:


Example:


Math Constructor

Unlike other global objects, the Math object has no constructor. Methods and properties are static.

All methods and properties (constants) can be used without creating a Math object first.


Math Object Methods


Method Description

  • abs(x) Returns the absolute value of x

  • acos(x) Returns the arccosine of x, in radians

  • asin(x) Returns the arcsine of x, in radians

  • atan(x) Returns the between -PI/2 arctangent of x as a numeric value

and PI/2 radians

  • atan2(y, x) Returns the arctangent of the quotient of its arguments

  • ceil(x) Returns the value of x rounded up to its nearest integer

  • cos(x) Returns the cosine of x (x is in radians)

  • exp(x) Returns the value of Ex

  • floor(x) Returns the value of x rounded down to its nearest integer

  • log(x) Returns the natural logarithm (base E) of x

  • max(x, y, z, ..., n) Returns the number with the highest value

  • min(x, y, z, ..., n) Returns the number with the lowest value

  • pow(x, y) Returns the value of x to the power of y

  • random() Returns a random number between 0 and 1

  • round(x) Returns the value of x rounded to its nearest integer

  • sin(x) Returns the sine of x (x is in radians)

  • sqrt(x) Returns the square root of x

  • tan(x) Returns the tangent of an angle


JavaScript Random


Math.random()

  • Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive):

  • Math.random() always returns a number lower than 1.

Example:


JavaScript Random Integers


Math.random() used with Math.floor() can be used to return random integers.


Example:


JavaScript Booleans


A JavaScript Boolean represents one of two values: true or false.


Boolean Values


Very often, in programming, you will need a data type that can only have one of two values, like

  • YES / NO

  • ON / OFF

  • TRUE / FALSE

For this, JavaScript has a Boolean data type. It can only take the values true or false.


The Boolean() Function


You can use the Boolean() function to find out if an expression (or a variable) is true:


Example:



Comparisons and Conditions

  • The chapter JS Comparisons gives a full overview of comparison operators.

  • The chapter JS Conditions gives a full overview of conditional statements.

  • The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.


Booleans Can be Objects


Normally JavaScript booleans are primitive values created from literals:

var x = false;

But booleans can also be defined as objects with the keyword new:

var y = new Boolean(false);


Example:

Note:- Do not create Boolean objects. It slows down execution speed.



Comments


bottom of page