What are parameters in javascript

JavaScript Function Parameters

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

Function Parameters and Arguments

Earlier in this tutorial, you learned that functions can have parameters:

Function parameters are the names listed in the function definition.

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

Parameter Rules

JavaScript function definitions do not specify data types for parameters.

JavaScript functions do not perform type checking on the passed arguments.

JavaScript functions do not check the number of arguments received.

Default Parameters

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:

Example

Default Parameter Values

ES6 allows function parameters to have default values.

Example

If y is not passed or undefined, then y = 10.

Function Rest Parameter

The rest parameter (. ) allows a function to treat an indefinite number of arguments as an array:

Example

function sum(. args) <
let sum = 0;
for (let arg of args) sum += arg;
return sum;
>

let x = sum(4, 9, 16, 25, 29, 100, 66, 77);

The Arguments Object

JavaScript functions have a built-in object called the arguments object.

The argument object contains an array of the arguments used when the function was called (invoked).

This way you can simply use a function to find (for instance) the highest value in a list of numbers:

Example

x = findMax(1, 123, 500, 115, 44, 88);

function findMax() let max = -Infinity;
for (let i = 0; i < arguments.length; i++) if (arguments[i] > max) max = arguments[i];
>
>
return max;
>

Or create a function to sum all input values:

Example

x = sumAll(1, 123, 500, 115, 44, 88);

function sumAll() let sum = 0;
for (let i = 0; i < arguments.length; i++) sum += arguments[i];
>
return sum;
>

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

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.

Источник

What is a JavaScript Function Parameter?

Javascript Course - Mastering the Fundamentals

In Javascript, Despite having a major difference, the phrases «parameters» and «arguments» of a function» are frequently used synonymously. The names defined in the function definition are known as function parameters . The javascript function parameters are part of the definition of a function. We can give multiple parameters inside a function which is separated by commas inside parentheses.

Syntax

Example

Let’s take a code to understand the concept of javascript function parameters in which we will find the largest element among the given element:

Explanation:

Firstly, we have made a function named «max_3num» which takes 3 parameters and returns the maximum value out of those numbers; then, we have used nested if else loop to check which number is greater among all those three numbers.

Function Arguments in JavaScript

A function’s argument is the value we supply when calling a function. Real arguments are provided whenever we call the function in the program.

For example, suppose a block of code:

Here, 12 and 15 are the arguments passed to the «xyz» function.

Let’s sum up the concept of argument and parameter together inside a block of code:

We will write a program in which we will create a function that will take two arguments and multiply both the numbers and return the resulting value:

Example

Explanation:

Here, we have made a function named «mulNumber» in which (a,b) are the javascript function parameters and the value (12,20) are the function argument which is passed then it gives multiplication of both the numbers as the output.

Function Parameter Rules in JavaScript

  • Data types for parameters are not specified in JavaScript function declarations.
  • The given arguments are not type checked by JavaScript functions.
  • The number of received arguments is not checked by JavaScript functions.

Parameters

Name

The name of the function is specified using it.

Arguments

It is provided in the function’s argument field.

Adding Function Parameters in JavaScript

We can add multiple javascript function parameters in our Javascript function according to your program requirements.

For instance, we will create a function named «demo()«, which will have zero parameters and will return a string.

Now, we will create a function named «demo1()«. It will take a single parameter, «name,» and when this function is called, a string containing the provided «name» argument will be returned:

Now we will create another function named subNumber(). The subNumber() will take two arguments passed in the «a» and «b» parameters, and it will return their difference:

Now, let’s call the all the three functions we have made above by passing the values as an argument to the functions:

Unmatched Function Parameters in JavaScript

When the number of arguments you pass is greater than or less than the number of parameters, the JavaScript function won’t throw an error.

For example, suppose we have created a function subNumber() which takes 2 parameter, «a» and «b»

Code

The subNumber() JavaScript function will not throw an error if it is called without any arguments. The additional arguments, however, will be initialized as «undefined«.

So, in the above situation, when we call the function without passing any argument, then «a» and «b» are initialized as «undefined«.

The JavaScript interpreter will, however, ignore any additional arguments if the number of arguments is more than the number of parameters.

Now suppose we pass a single argument, that is, «a» as a parameter, then «b» will be initialized as «undefined«.

Suppose we have passed more arguments than the defined parameter in the function; than, in this condition, the «a» and «b» will be initialized with the values, and the third argument will be ignored.

Concluding all the above three cases which is shown below:

Code

Default Function Parameters in JavaScript

You can specify the javascript function parameter default values in JavaScript as well. In the function declaration, the assignment operator «=» is used to combine these values.

Let’s take an example to understand the concept of default function:

Example

Explanation

Here in the above case, in subNumber() function we have assigned «0» to both the variables of our function, that is «a» and «b». So, now if we don’t pass any argument to the function, it will initialize and it’s value to be zero.

Using Argument Object in JavaScript

There is a built object in Javascript functions called an Argument object. The arguments of a function are maintained in an array-like object. You can refer to the arguments supplied to a function in the following ways:

where j is the ordinal number starting from 0, so, the first argument passed to a function would be arguments[0] .

Similarly, the total number of arguments is indicated by

You can call a function with more parameters than it is explicitly specified to take by using the arguments object.

Let’s consider an example in which we will multiply the arguments of a function:

Code

Explanation

Here, we have made a function named «mulArgs» which takes the argument passed and treats it as an array of elements, and then initializes a variable named «mul» to 1 then a for loop is used to iterate through the length of argument and in the for loop each element of the loop is multiplied and then returned as an output.

Pass by Value and Pass by Reference in JavaScript

Pass by Value

When a parameter is sent by value in JavaScript, a copy of the parameter’s real value is created in memory (a new memory allocation is established), and all changes are applied to that new value (i.e., copied value). The original value and the copied value are independent of one another since they both occupy a separate amount of memory; as a result, changing the value of the variable inside the function has no impact on the variable outside the function.

Read our Pass By Value vs. Pass By Reference in JavaScript article for detailed information. Let’s take an example to understand the concept of Pass the value:

Explanation:

In the above code, we have created a variable named res1 and assigned a value of 25 to it, which creates space in the memory by the name res1 and suppose its address is 1001. Then we create a variable res2 and assign res1 to it; subsequently, the equals operator detects that we are working with primitive values and builds a new space in memory with address 1002 and gets a copy of res1 value i.e., 25. Now, we can see that both the variables have the same value(i.e., 25), but both of them have different spaces. So if a change is done in one variable, then it does not affect another variable value.

Pass by Reference

In the javascript function parameter, instead of creating a new memory location like the pass by value does in JavaScript, pass-by-reference allows the function to retrieve the variable’s initial value by passing the reference or address of the actual parameter. Therefore, the original value also changes if we modify the value of the variable inside the function.

Let’s take an example to understand the concept of Pass the reference:

Explanation:

Here, we call the demo(student) function, and a new variable «obj» will be created, which will get details from the student object. Both student and obj variables refer to the same object. So, in such cases, any changes done to the «obj» will be reflected in the «student».

Conclusion

  • In the javascript function parameter, the terminology related to the function in JavaScript is arguments and parameters .
  • The variables listed in the function definition are known as function parameters, and the values supplied as parameters are known as function arguments.
  • We can use the default parameter when the arguments are missing when the function is called.
  • The function’s arguments are stored in a special object called arguments that resembles an array.
  • In JavaScript, the pass-by-value creates a copy of the original variable in order to prevent any modifications to the copied variable from affecting the original variable.
  • In JavaScript, pass the reference is defined as the real parameter when we pass by reference. There is no memory creation of a copy.

Источник

Читайте также:  Java чтение файла до пробела
Оцените статью