How to Destructure JavaScript Object Properties Having Dashes?

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'

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.