Math object in JavaScript

389 Views Asked by At

In JavaScript, Math object doesn't have an constructor, but why it's capitalized such as Math.random()? As I have understood, objects such as Date and Object get capitalized because they have constructors (e.g. Date.now())

1

There are 1 best solutions below

0
On BEST ANSWER

Initial caps capitalization isn't only used for constructor functions, though that's the predominant use. It's also used for objects like Math and Reflect, values like Infinity, and the Symbol function (Symbol creates symbols, but isn't a constructor — you don't use new with it). It differentiates them from local variables.

A lot of this was put together in a frenzied 10 days in May in 1995 by Brendan Eich, who probably didn't worry too much about what he was and wasn't capitalizing given the deadline he was working to. :-)

(There's an argument that Infinity and NaN should be infinity and nan to match undefined and null, but...consistency is hard. Then there's the whole thing that Infinity, NaN, and undefined are global constants but null is a keyword...)