Echo JS 0.11.0

<~>
kali 1456 days ago. link parent 5 points ▲ ▼
> the solution is not good as it will result in 'a' to match instead of 'aaa' as 'a' included in 'a' but not equal to.

Not really, it won't.

You may be confusing Array#includes and String#includes or something, but just try it in the console:

    > ['aaa', 'aa'].includes('a')
    false

The final part of the article implies editableNoteTypes is still an array, which still works as expected:

    > let editableNoteTypes = ['aaa', 'aa'];
    > editableNoteTypes.includes('a')
    false


---

Still, I do agree that the proposed solution is not good. Using Array#includes does not really improve readability. And to boot it only applies to a very specific conditional where you're doing exactly "x === a || x === b || x ==== c" but no other kind of combination.

Instead, if you do want to improve readability, you should give that check a name. Put it into a function and then use that function.

    function isEditableNote(type) {
        // whatever...
    }

    // then...
    if (isEditableNote(noteType)) {
        // ...
    }

Replies

alonronin 1455 days ago. link 2 points ▲ ▼
yep, you are correct, I confused with string.includes :-)