Default Parameters for Functions

You can pass in default parameters for functions so that if the function is used with no parameter it will use the default one.

Demo

JS

function run_function(foo = "Blah!") {
  var my_result = foo;
  write_result(my_result);
  return my_result;
}

// this is a common function just to write the content into the result div
function write_result(result) {
  var result_html = document.getElementById("result");
  result_html.innerHTML = result;
}

HTML

<div id='result' class='mb-3 alert alert-primary'>..</div>
<button onclick='run_function();' class='btn btn-primary mb-1'>Run Function (using default)</button>
<button onclick='run_function("Something Else");' class='btn btn-primary mb-1'>Run Function (using other text)</button>

Working Result

..

Other Items in javascript
Page Views

This page has been viewed 38 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹