DEV Community

AidanLincoln
AidanLincoln

Posted on

4 1

ES6 Required Parameters

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"

Top comments (0)

Auth0

Easy to implement, endless possibilities

With a few lines of code, you can have Auth0 by Okta integrated in any app written in any language and any framework. πŸš€ Start building for free today.

Sign up now

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay