Do JavaScript functions need parameters?

error handling JavaScript parametersCode Snippets JavaScript Required Parameters for Functions in JavaScriptRequired Parameters for Functions in JavaScriptChris Coyier on Jan 2, 20

Do JavaScript functions need parameters?

error handling JavaScript parametersCode Snippets JavaScript Required Parameters for Functions in JavaScript

Required Parameters for Functions in JavaScript

Avatar of Chris Coyier
Avatar of Chris Coyier

Chris Coyier on Jan 2, 2020

Ooo this is clever! Im snagging this from Davids blog.const isRequired = () => { throw new Error('param is required'); }; const hello = (name = isRequired()) => { console.log(`hello ${name}`) }; // These will throw errors hello(); hello(undefined); // These will not hello(null); hello('David');

The idea here is that it uses default parameters, like how the b parameter here has a default if you dont send it anything:function multiply(a, b = 1) { return a * b; }

So above, if you dont provide a name, itll use the default instead, which is that function that throws an error.

Video liên quan