ES6 Optional Parameters

by andyjmeyers

HTML

<div id='test1'></div>
<div id='test2'></div>
<div id='test3'></div>
<div id='test4'></div>
<div id='test5'></div>

JavaScript

var count = 0;
function helperFunc(){
    count++;
    return "OPTIONAL BAR ->" + count;
}

function helloWorld(foo = "OPTIONAL FOO", bar=helperFunc()){
    return "{foo: " + JSON.stringify(foo) + ", bar: " +
        JSON.stringify(bar) + "}"; 
}

document.getElementById('test1').innerHTML = helloWorld("foo", "bar");
document.getElementById('test2').innerHTML = helloWorld(undefined, "bar");
document.getElementById('test3').innerHTML = helloWorld(null, "bar");
document.getElementById('test4').innerHTML = helloWorld("foo");
document.getElementById('test5').innerHTML = helloWorld();