Object literals are a preferred way of passing a large number of optional arguments to a function:
function displayInfo(args) { var output = ""; if (typeof args.name == "string"){ output += "Name: " + args.name + "\n"; } if (typeof args.age == "number") { output += "Age: " + args.age + "\n"; } console.log(output); } displayInfo({ name: "First", age: 29 }); displayInfo({ name: "new Name" });
Here, the function displayInfo() accepts a single argument named args.
The argument may come in with a property called name or age or both or neither of those.
The function tests for the existence of each property using the typeof operator and then to construct a message to display based on availability.