Math.random():
Math.random() is a built-in function that returns a floating-point number between 0 (inclusive) and 1 (exclusive). This means the result is always >= 0 and < 1.
Basic usage
const randomNumber = Math.random();
console.log(randomNumber); // e.g., 0.34784310291847
Get a random number between 0 and a specific number:
const randomUpTo10 = Math.random() * 10; // 0 <= result < 10
Math.Floor
Math.floor() is a method that rounds a number down to the nearest integer.
Examples:
Math.floor(4.9); // 4
Math.floor(4.1); // 4
Math.floor(4.0); // 4
Math.floor(-4.1); // -5
Math.floor(-4.9); // -5
Array in JS:
Array is a data structure used to store multiple values in a single variable.
Creating an Array
const fruits = ["apple", "banana", "cherry"];
const numbers = [1, 2, 3, 4, 5];
Length:
The length property sets or returns the number of elements in an array.
Syntax:
array.length
Set the length of an array:
array.length = number
Top comments (0)