Sricharan Krishnan
Sricharan Krishnan

Reputation: 100

"".constructor vs 2.constructor in Javascript

I've just been experimenting with a little bit of Javascript and got to the point of understanding the inheritance concept.

I am able to get an evaluation for the below code:

"".constructor
//which evaluates to function String()

Ok Cool. But why is it that when I do the below code, there is an error?

2.constructor
//returns an error

Basically both are primitives right?, so should there not be an error for the empty string as well?

Hope someone can give me a good explanation that will help me learn this one better. Looking forward to your support.

Upvotes: 1

Views: 47

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386680

You could spend another dot for the decimal point.

console.log(2..toString());
console.log(2.2.toString());

Or wrap the value in parenthesis.

console.log((2).toString());
console.log((2.2).toString());

Upvotes: 1

Related Questions