Search⌘ K
AI Features

Discussion: What’s the Value of Math?

Discover how to leverage JavaScript's valueOf property by assigning Math.random to create custom objects that behave like random number generators. Learn to understand the output of random numbers and use bitwise operators to manipulate these values. This lesson helps enhance your knowledge of JavaScript objects and clever coding techniques.

Verifying the output

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

Javascript (babel-node-es2024)
const r = {valueOf:Math.random},
a = [r*11|0,r*11|0,r*11|0];
console.log(a);

The valueOf property

JavaScript has a handy property called valueOf that automatically gets called when an object needs to be converted to a primitive value. By assigning Math.random to the valueOf property of an object, we can create a ...