Skip to content

Instantly share code, notes, and snippets.

@dimitropoulos
Last active November 1, 2022 12:13
Show Gist options
  • Save dimitropoulos/56f6834ce11ddf59f56b4a6e1ec10523 to your computer and use it in GitHub Desktop.
Save dimitropoulos/56f6834ce11ddf59f56b4a6e1ec10523 to your computer and use it in GitHub Desktop.
Take the argument destructuring challenge

Take the argument destructuring challenge

const quiz1 = ({ a, b = 2 }) => b;
const quiz2 = ({ a, b = 2 } = {}) => b;
const quiz3 = ({ a, b = 2 } = { b: 3 }) => b;
const quiz4 = ({ a, b } = { b: 3 }) => b;
const quiz5 = ({ a, b }) => b;
quiz1 quiz2 quiz3 quiz4 quiz5
quiz() ? ? ? ? ?
quiz({}) ? ? ? ? ?
quiz({ a: 1 }) ? ? ? ? ?
quiz(null) ? ? ? ? ?

[copy and paste this below]

|                  | `quiz1` | `quiz2` | `quiz3` | `quiz4` | `quiz5` |
| -                | -       | -       | -       | -       | -       |
| `quiz()`         | ?       | ?       | ?       | ?       | ?       |
| `quiz({})`       | ?       | ?       | ?       | ?       | ?       |
| `quiz({ a: 1 })` | ?       | ?       | ?       | ?       | ?       |
| `quiz(null)`     | ?       | ?       | ?       | ?       | ?       |
@dimitropoulos
Copy link
Author

dimitropoulos commented Oct 28, 2022

And for those in a hurry:

Answers
Are you sure you wanna see the answers?
For real, it's no fun if you don't try for yourself first
I see I see, you're a 10x JavaScript ninja, and you would get them all right anyway, gotcha..
HERE YOU GO.

Exactly one of these cells is wrong. RIP.

quiz1 quiz2 quiz3 quiz4 quiz5
quiz() TypeError 2 3 3 3
quiz({}) 2 2 3 undefined undefined
quiz({ a: 1 }) 2 2 3 undefined undefined
quiz(null) TypeError TypeError TypeError TypeError TypeError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment