JS ...rest syntax demo
by umurkaragoz
HTML
<h3>
JS <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters" target="_blank">...rest syntax</a> demo
</h3>
<b id="first-arg"></b>
<ul id="argument-list">
</ul>
JavaScript
function argumentTest(first, ...rest) {
document.querySelector('#first-arg').innerText = first;
let argumentList = document.querySelector('#argument-list');
// loop through the rest of the parameters
for(let arg of rest){
let newLi = document.createElement('li');
newLi.innerText = arg;
argumentList.appendChild(newLi);
}
}
// call your function with any number of arguments
argumentTest("first arg", "#2", "more arguments", "this is not an argument but a contradiction", 1, 23454, 4564746);