Skip to main content

Command Palette

Search for a command to run...

Javascript E๐—บ๐—ฝ๐˜๐˜† ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜ ๐—ฎ๐˜€ ๐—ฎ ๐—ณ๐—ฎ๐—น๐—น๐—ฏ๐—ฎ๐—ฐ๐—ธ ๐˜„๐—ต๐—ถ๐—น๐—ฒ ๐—ฑ๐—ฒ๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ถ๐—ป๐—ด

Published
โ€ข1 min readโ€ขView as Markdown
Javascript E๐—บ๐—ฝ๐˜๐˜† ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜ ๐—ฎ๐˜€ ๐—ฎ ๐—ณ๐—ฎ๐—น๐—น๐—ฏ๐—ฎ๐—ฐ๐—ธ ๐˜„๐—ต๐—ถ๐—น๐—ฒ ๐—ฑ๐—ฒ๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ถ๐—ป๐—ด
T

I am a developer from Cape Town,South Africa

As explained in one of my posts in the Javascript series, you can use the optional chaining operator to simplify and avoid breaking code

So the below code:

๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ต ๐˜ถ๐˜ด๐˜ฆ๐˜ณ = {
๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ: '๐˜‹๐˜ข๐˜ท๐˜ช๐˜ฅ'
};

๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ต ๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜•๐˜ข๐˜ฎ๐˜ฆ = ๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ญ๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ && ๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ญ๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ.๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต && ๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ญ๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ.๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต.๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ;

is same as:

๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ต ๐˜ถ๐˜ด๐˜ฆ๐˜ณ = {
๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ: '๐˜‹๐˜ข๐˜ท๐˜ช๐˜ฅ'
};

๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ต ๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜•๐˜ข๐˜ฎ๐˜ฆ = ๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ญ๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ?.๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต?.๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ;

In the above code, ๐˜€๐˜๐—ฟ๐—ฒ๐—ฒ๐˜๐—ก๐—ฎ๐—บ๐—ฒ will be ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ณ๐—ถ๐—ป๐—ฒ๐—ฑ as ๐˜€๐˜๐—ฟ๐—ฒ๐—ฒ๐˜ property does not exist on the user object.

This is because the optional chaining operator returns ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ณ๐—ถ๐—ป๐—ฒ๐—ฑ if the property does not exist.

And destructuring on ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ณ๐—ถ๐—ป๐—ฒ๐—ฑ will throw an error.

So If you're using destructuring along with the optional chaining operator, you will get an uncaught error If you try to use destructuring like this:

๐˜ช๐˜ง (๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ) {
๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ต { ๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ, ๐˜ข๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด } = ๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ญ๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ?.๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต; // ๐™๐™ฃ๐™˜๐™–๐™ช๐™œ๐™๐™ฉ ๐™๐™ฎ๐™ฅ๐™š๐™€๐™ง๐™ง๐™ค๐™ง: ๐˜พ๐™–๐™ฃ๐™ฃ๐™ค๐™ฉ ๐™™๐™š๐™จ๐™ฉ๐™ง๐™ช๐™˜๐™ฉ๐™ช๐™ง๐™š ๐™ฅ๐™ง๐™ค๐™ฅ๐™š๐™ง๐™ฉ๐™ฎ '๐™ฃ๐™–๐™ข๐™š' ๐™ค๐™› ๐™ช๐™ฃ๐™™๐™š๐™›๐™ž๐™ฃ๐™š๐™™
}

To fix this, you need to assign an empty object all the time while destructuring like this:

๐˜ช๐˜ง (๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ) {
๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ต { ๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ, ๐˜ข๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด } = ๐˜ถ๐˜ด๐˜ฆ๐˜ณ.๐˜ญ๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ?.๐˜ด๐˜ต๐˜ณ๐˜ฆ๐˜ฆ๐˜ต || {}; // ๐™ฌ๐™ค๐™ง๐™ ๐™จ ๐™›๐™ž๐™ฃ๐™š, ๐™ฃ๐™ค ๐™š๐™ง๐™ง๐™ค๐™ง
๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ฐ๐˜ญ๐˜ฆ.๐˜ญ๐˜ฐ๐˜จ(๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ, ๐˜ข๐˜ฅ๐˜ฅ๐˜ณ๐˜ฆ๐˜ด๐˜ด); // ๐˜ถ๐˜ฏ๐˜ฅ๐˜ฆ๐˜ง๐˜ช๐˜ฏ๐˜ฆ๐˜ฅ ๐˜ถ๐˜ฏ๐˜ฅ๐˜ฆ๐˜ง๐˜ช๐˜ฏ๐˜ฆ๐˜ฅ
}

More from this blog

The simplest and modern to avoid an error while displaying fetched data in Javascript

๏ฟฝ ๐—ง๐—ต๐—ฒ ๐˜€๐—ถ๐—บ๐—ฝ๐—น๐—ฒ๐˜€๐˜ ๐—ฎ๐—ป๐—ฑ ๐—บ๐—ผ๐—ฑ๐—ฒ๐—ฟ๐—ป ๐˜„๐—ฎ๐˜† ๐˜๐—ผ ๐—ฎ๐˜ƒ๐—ผ๐—ถ๐—ฑ ๐—ฒ๐—ฟ๐—ฟ๐—ผ๐—ฟ ๐˜„๐—ต๐—ถ๐—น๐—ฒ ๐—ฑ๐—ถ๐˜€๐—ฝ๐—น๐—ฎ๐˜†๐—ถ๐—ป๐—ด ๐—ณ๐—ฒ๐˜๐—ฐ๐—ต๐—ฒ๐—ฑ ๐—ฑ๐—ฎ๐˜๐—ฎ ๐—ถ๐˜€ ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐—ก๐˜‚๐—น๐—น๐—ถ๐˜€๐—ต ๐—ฐ๐—ผ๐—ฎ๐—น๐—ฒ๐˜€๐—ฐ๐—ถ๐—ป๐—ด ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ (??) โœ”๏ธ ๐˜›๐˜ฉ๐˜ฆ ๐˜ฏ๐˜ถ๐˜ญ๐˜ญ๐˜ช๐˜ด๐˜ฉ ๐˜ค๐˜ฐ๐˜ข๐˜ญ๐˜ฆ๐˜ด๐˜ค๏ฟฝ...

Jun 3, 20221 min read
The simplest and modern to avoid an error while displaying fetched data in Javascript

4 Helpful Javascript Array tricks you should know

An array is one of the most common concepts of Javascript, which gives us a lot of possibilities to work with data. Check these tricks which can be very helpful! ๐Ÿ‘‰ ๐—Ÿ๐—ฒ๐˜โ€™๐˜€ ๐—ด๐—ฒ๐˜ ๐˜€๐˜๐—ฎ๐—ฟ๐˜๐—ฒ๐—ฑ ๐Ÿง‘โ€๐Ÿ’ป ๐ŸŒถ๏ธ ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฟ๐—ฒ๐—บ๐—ผ๐˜ƒ๐—ฒ ๐—ณ๐—ฎ๐—น๐˜€๐˜† ๐˜ƒ๐—ฎ๐—น๏ฟฝ...

Jun 2, 20221 min read
G

GenesisDigitalZA

5 posts