js get arguments code example

Example 1: javascript check if argument is passed

function func(arg1, arg2) {
  if (typeof arg2 === "undefined") {
    arg2 = "defaultValue";
  }
  
  //Rest of function
}

Example 2: js unspecified parameters

function my_log(...args) {
     // args is an Array
     console.log(args);
     // You can pass this array as parameters to another function
     console.log(...args);
}

Example 3: how to access any argument in javascript

function example() {
	console.log(arguments);
  	console.log(arguments[0]);
} // Console outputs an array of each argument with its value

example('hi', 'hello'); 
// Outputs: 
// ['hi', 'hello']
// 'hi'