DEV Community

loading...

ES6 Required Parameters

aidanlincoln profile image AidanLincoln ・1 min read

Have you ever made a function that should require parameters to be passed in? Probably. In ES6 you can use default parameters to achieve this goal.

First you can make a function that returns an error message. This error function can be used as a default parameter in any function you make.
Now if you call the function you made without providing the necessary parameters, an error will be thrown.

Example:

function error() {
throw new Error("Missing parameter");
}
function test(parameter = error()) {
return parameter;
}

test() // "Missing parameter"
test("hi") // "hi"

Discussion (1)

pic
Editor guide