Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Math Object does not need new keyword in javascript

I have know that Math is an Object in javascript, however in the book it says when use the Math Object, we do not need to use the new keyword.For example,

var pi = Math.PI;
alert(pi);

I want to know why it does not need, and in other Object, like Date, it needs the new keyword.

like image 631
user4212562 Avatar asked Feb 10 '26 21:02

user4212562


1 Answers

typeof Math -->    "object"
typeof Date -->    "function"

Math is an Object and Date is constructor function.

new key word is used to initialize an instance with a constructor function not with objects.

like image 107
Sarath Avatar answered Feb 13 '26 14:02

Sarath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!