WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Function Parameters


JavaScript function does not perform any checking on parameter values (arguments).


Function Parameters and Arguments

functions can have parameters:

functionName(parameter1, parameter2, parameter3) {
    code to be executed
}

Parameter Defaults

If a function is called with missing arguments (less than declared), the missing values are set to: undefined

Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter:.

If a function is called with too many arguments (more than declared), these arguments can be reached using the arguments object.


function myFunction(x, y) {
    if (y === undefined) {
          y = 0;
    } 
}


The Arguments Object

Example

x = sumAll(1, 123, 500, 115, 44, 88); function sumAll() { var i; var sum = 0; for (i = 0; i < arguments.length; i++) { sum += arguments[i]; } return sum; }

Arguments are Passed by Value .

Changes to arguments are not visible (reflected) outside the function.