Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Destructuring primitive values in JavaScript

The doc page for destructuring assignment says

This means if you try to destruct a primitive value, the value will
get wrapped into the corresponding wrapper object and the property is
accessed on the wrapper object.

Does this mean the following code?:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const { a, toFixed } = 1;

Is equivalent to:

const { a, toFixed } = {1};

>Solution :

No, it means that your 1 is turned into a Number instance, and then the destructuring acts on that.

So it’s like

const { a, toFixed } = new Number(1);

The primitive value types number, string, and boolean all have corresponding wrapper types.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading