Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor for null and undefined

Tags:

javascript

Of the five primitive JavaScript data types (string, number, boolean, null and undefined), the first three have associated constructors:

new String("Hello!")
new Number(40)
new Boolean(true)

The constructed objects are wrappers for the object literals. In particular, new String("Hello!") === "Hello!" evaluates to false.

Are there a similar constructors for the null and undefined primitive data types that produce object wrappers different from the corresponding object literal?

like image 820
Randomblue Avatar asked Aug 11 '26 12:08

Randomblue


1 Answers

No, there are not, and it wouldn't make sense considering the "values" they represent.

Ignore the fact that typeof null === 'object'

According to MDN:

JavaScript automatically converts primitives and String objects, so that it's
possible to use String object methods for primitive strings.

The interpreter just does some magical auto-wrapping of primitive in classes if you call a method that affects the primitive, like charAt.

like image 165
Andy Ray Avatar answered Aug 13 '26 02:08

Andy Ray



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!