DEV Community

Discussion on: JavaScript Katas: Polish Alphabet

Collapse
 
kosich profile image
Kostia Palchyk

Nice :)

We can also replace chars that we don't know with either mapped char or with a ?

'Wół'.replace(/[^a-zA-Z]/g, c => mapping[c] ?? '?')
Collapse
 
miku86 profile image
miku86

Hey Kostia,

currently trying to understand your code.
Do you mean || instead of ??.

Collapse
 
kosich profile image
Kostia Palchyk • Edited

Hey, Michael!

Yeah, in this particular case || would work as well as ??.
Nullish coalesce would be useful if you have a mapping that simply drops some char, e.g:

  const mapping = {
    ą: "a",
    ć: "c",
    ę: "", // <-- drop ę
    ł: "l",
    // etc ...
  };

Here || wont work. But that's just a case I made up 🙂

Thread Thread
 
miku86 profile image
miku86

Awesome, thanks for that!

I didn't know that JavaScript added a nullish coalescing operator in ES2020!
Currently running Node 12, I should probably update it, too!

Thread Thread
 
kosich profile image
Kostia Palchyk

Yeah, JS is getting hard to track :)

Also note ?. conditional chaining — helps a lot!

And they've added more operators just recently (@ stage 4):

||= &&= ??= for conditional assignment (dunno why...)

article on recent updates: dev.to/hemanth/stage-4-features-5a26

not mentioning other #, |>, {| }, etc early stage propositions — 🤯

Thread Thread
 
miku86 profile image
miku86

Thanks for the summary!

I already use the ?. in my TypeScript projects,
will have a look at the other ones!