Methods and Functions in JS


why Math.random() is a method but Math.floor() is a function?


This contradicts what you just said about Math.random(). The fact is, they are both methods of the same object, or class. However in terms of usage, they are both treated as functions since they do not have any instances. We would never expect to see:

const math = new Math()

Above, math is an instance of the Math class, but as we know, this is not how we implement the Math object.

In other words, both methods as treated as built in functions to which we supply the argument (in the case of floor). If we attempt to supply an argument to Math.random() it will be ignored.

Bottom line, all of the methods of the Math object are essentially built in functions, those that are not constants such as, Math.Pi and Math.E.


Hello, when I was in that course sometimes I had same doubts, so I recommend you to investigate each method that you learn in the course, I did it and it was useful, now I know many methods almost for any situation, you can use mozilla’s site, I hope it helps you like me :grinning:

Math.floor() - JavaScript | MDN