JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Function Parameters

A function can have one or more parameters, which will be supplied by the calling code and can be used inside a function.

JavaScript is a dynamic type scripting language, so a function parameter can have value of any data type.

Function Parameters and Arguments

Function parameters are the names listed in the function definition.

Function arguments are the real values passed to (and received by) the function.

Function Parameters

You can specify parameters when you define your function to accept input values at run time.

Function parameters are listed inside the parentheses () in the function definition.

Run code

You can define as many parameters as you like.

Run code

However for each parameter you specify, a corresponding argument needs to be passed to the function when it is called, otherwise its value becomes undefined.

Run code

Default Parameters

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

This means that if no arguments are provided to function when it is called these default parameters values will be used.

Run code

The Arguments Object

All the functions in JavaScript can use arguments object by default. An arguments object includes value of each parameter.

The arguments object is an array like object. You can access its values using index similar to array.

The following example returns the largest number from passed arguments:

Run code

The following example returns the sum of all passed arguments:

Run code

An arguments object is valid even if function does not include any parameters.

The arguments object is an array like object however, it does not support array methods.

Rest Parameters

The rest parameter syntax allows us to pass an indefinite number of arguments to a function as an array.

This is particularly helpful in situations when you want to pass arguments to a function but you have no idea how many you will need.

A rest parameter is specified by prefixing a named parameter with rest operator (...) i.e. three dots.

Run code

Arguments are Passed by Value

The parameters, in a function call, are the function's arguments.

JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations.

If a function changes an argument's value, it does not change the parameter's original value.

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

Objects are Passed by Reference

In JavaScript, object references are values.

Because of this, objects will behave like they are passed by reference:

If a function changes an object property, it changes the original value.

Changes to object properties are visible (reflected) outside the function.