Math.random() returns undefined

i have this code to generate a random number, but it returns undefined. anyone knows why it says so? thanks.

let NUMBER={
SIZE: 9
};

const random = () => {
Math.floor(Math.random() * NUMBER.SIZE);
};

let digit = random();
console.log(digit)

When you use a code block with an arrow function, it works like a “regular” function, and you need to use the return statement:

const random = () => {
  return Math.floor(Math.random() * NUMBER.SIZE);
};

If you didn’t add curly braces (so no code block) then whatever it evaluates to will be what is returned:

const random = () =>
  Math.floor(Math.random() * NUMBER.SIZE);

or

const random = () => Math.floor(Math.random() * NUMBER.SIZE);
1 Like

thanks, it had given me a hard time processing to my little understanding. i felt i was getting it. thank you for the explanation, the correction works!

This is confusing stuff, it takes time.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.