Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I would like a JavaScript function to have optional arguments which I set a default on, which get used if the value isn't defined (and ignored if the value is passed). In Ruby you can do it like this:

def read_file(file, delete_after = false) 

# code end

Does this work in JavaScript?

function read_file(file, delete_after = false) { 

// Code 

}

1 Answer

0 votes
by (106k points)

From the release of ES6, the default parameters are in the language specification.

The default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

function read_file(file, delete_after = false) { 

// Code

}

Related questions

0 votes
1 answer
+2 votes
1 answer
0 votes
1 answer

Browse Categories

...