How to Destructure JavaScript Object Properties Having Dashes?

Destructure JavaScript object properties having dashes in the name

You can't directly destructure a JavaScript property that has dashes/hyphens in the name. To be able to destructure such properties you will have to rename them, for example, like so:

const { 'dashed-prop': renamedDashedProp } = obj;

For a complete example, consider the following:

const headers = {
    'content-type': 'text/html',
    'content-length': 1024,
    expires: 'Thu, 21 Nov 2024 00:00:00 GMT',
};

const { 'content-type': contentType } = headers;

console.log(contentType); // 'text/html'

Hope you found this post useful. It was published . Please show your love and support by sharing this post.