Day-5 in JS: Understanding Math.random & Math.floor, Array, Length property..

day-5-in-js:-understanding-mathrandom-&-mathfloor,-array,-length-property.

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 
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
3-reasons-you-need-the-skills-to-manage-multiple-projects

3 Reasons you need the skills to manage multiple projects

Next Post
introducing-jawbone-sockets

Introducing Jawbone Sockets

Related Posts