Search⌘ K
AI Features

Discussion: Dating with Math

Explore how JavaScript's Date object can be used with the modulo operator to generate numbers within a range. Understand why this method isn't truly random and how Math.random combined with Math.floor creates reliable random integers. This lesson helps you grasp these concepts to better handle date and random number operations in JavaScript.

Verifying the output

Now, it’s time to execute the code and observe the output.

Javascript (babel-node-es2024)
const date = new Date;
const x = date % 100;
console.log(x);

The Date object

In JavaScript, the Date object represents a specific point in time. When we create a new instance of the Date object using the new keyword, it initializes with the current date and time.

Understanding the output

Now, what this code does is take that current date and time and divide it by 100. This approach works because when we perform integer math on a new Date ...