Open In App

What is the use of the Math.random() function in JavaScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Math.random() function is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).

 Syntax:

Math.random();

Example: Here, Math.random() generates a random number, which could be any floating-point value between 0 (inclusive) and 1 (exclusive). This random number is then stored in the variable randomNumber, and its value is printed to the console.

Javascript




let randomNumber =
    Math.random();
console.log(randomNumber);


Output

0.09157103247687903

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads