Esbjörn Blomquist
1 min readSep 20, 2021

--

It could be argued that it is not destructuring that makes the example throw an error, it is default parameters. It is a known fact that "null breaks default parameters". But destructuring works in the same way as default parameters. This is actually because it is the correct way, but let's look at that:

- "undefined" in JavaScript means that the value is not yet set, it is not defined.

- Null is a value that exists for when you want to signal that the value is actually set, but for some reason you want to indicate that it could not get a proper value. It is different from undefined and does not mean the same thing (and should not in your code).

Acknowledging the meanings and differences between these two, both default parameters & destructuring works as intended. Null should simply not be used in JavaScript. Use ESLint to prevent it (https://www.npmjs.com/package/eslint-plugin-no-null).

--

--

Esbjörn Blomquist
Esbjörn Blomquist

Written by Esbjörn Blomquist

Software architect and developer with web and functional JavaScript close to his heart. Worked many years with web solutions mainly within the IoT field.

Responses (1)