This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
Suppose you have a condition like the following:
if (wizard === 'Merlin' || wizard === 'Gandalf') {
// ...
}
My mind needs a moment to process this if
. Additionally, the condition becomes harder to read if there are more wizards and you have to chain all these logical ORs.
And now look at what Chris advises to use instead:
if (['Merlin', 'Gandalf'].includes(wizard)) {
// ...
}
Is that readable code, or what? 😲 The condition even includes the word include
to make it easier to understand! 👏 It's a tiny change that improves readability tremendously.
I'll adopt this pattern from now on! Thanks, Chris.
Reply to Stefan
This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Stefan Judis | Sciencx (2022-02-22T23:00:00+00:00) Readable JavaScript conditions (#note). Retrieved from https://www.scien.cx/2022/02/22/readable-javascript-conditions-note/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.