Syntax
Math.random()Examples
Basic Random Number
Generating a raw random decimal between 0 and 1.
console.log(Math.random()); // e.g. 0.7234891...
console.log(Math.random()); // a different value each timeRandom Integer in a Range
The standard formula for generating a random whole number within bounds.
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(randomInt(1, 6)); // simulates a dice roll (1-6)
console.log(randomInt(1, 100)); // random number 1-100Picking a Random Array Element
A common practical use of Math.random().
const colors = ["red", "green", "blue", "yellow"];
function randomChoice(arr) {
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
console.log(randomChoice(colors)); // a random color from the arrayBest practices
- Use the Math.floor(Math.random() * (max - min + 1)) + min formula for inclusive random integers in a range
- Do not use Math.random() for anything security-related (tokens, passwords, keys) - use crypto.getRandomValues() instead
- Remember Math.random() never returns exactly 1, so ranges should account for that when calculating maximums
- Seed-based reproducible randomness is not built in - use a dedicated library if you need deterministic "random" sequences for testing
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Number Methods & Parsing
Number methods handle formatting and validation. toFixed() formats a number to a fixed number of decimal places, returning a string. parseInt() and parseFloat() convert strings to numbers, stopping at the first invalid character. Number.isInteger() and Number.isNaN() provide more reliable type checks than their global counterparts.Math Object: round, floor, ceil, abs, max, min, pow, sqrt
The Math object provides mathematical constants and functions as static methods - it is not a constructor, so you never create a Math instance. Common methods include rounding (round, floor, ceil, trunc), extremes (max, min), and power operations (pow, sqrt, cbrt).Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.
Number methods handle formatting and validation. toFixed() formats a number to a fixed number of decimal places, returning a string. parseInt() and parseFloat() convert strings to numbers, stopping at the first invalid character. Number.isInteger() and Number.isNaN() provide more reliable type checks than their global counterparts.Math Object: round, floor, ceil, abs, max, min, pow, sqrt
The Math object provides mathematical constants and functions as static methods - it is not a constructor, so you never create a Math instance. Common methods include rounding (round, floor, ceil, trunc), extremes (max, min), and power operations (pow, sqrt, cbrt).Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.