What is Math.random() in JavaScript?

Math is a built-in object in JavaScript that contains different methods and properties used to perform mathematical operations. It contains a random() function, which is used to return a random number in the range from 0 to 1.

Syntax

Math.random();

Return value

  • Number: The function returns a random number in range 0 (inclusive) to 1 (exclusive). It is of the Number type.

Example

// returns a random number from 0 to 1
console.log("random() = " + Math.random());
// returns a random number from 1 to 10
console.log("random()*10 = " + Math.random()*10);
// returns a random number starting from 5
console.log("random()+5 = " + (Math.random()+5));
// returns a random number starting from 5 to 15
console.log("random()*10+5 = " + (Math.random()*10+5));
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources