Different ways you can pass arguments to a function in JavaScript
By: Tanya in Javascript Tutorials on 2023-04-25
Here are the different ways you can pass arguments to a function in JavaScript, using the area
function example:
- Positional parameters: This is the traditional way of passing arguments to a function, where the values are listed in order of their corresponding parameters. For example:
function area(width, height) { return width * height; } area(10, 20); // returns 200
- Default parameters: You can provide default values for function parameters, which are used when no argument is provided or when
undefined
is passed as the argument. For example:
function area(width = 5, height = 10) { return width * height; } area(); // returns 50 area(3); // returns 30 area(4, 6); // returns 24
- Rest parameters: You can use the rest parameter syntax (
...
) to pass an arbitrary number of arguments as an array. For example:
function area(...dimensions) { let total = 1; dimensions.forEach(dimension => { total *= dimension; }); return total; } area(2, 3, 4); // returns 24 area(2, 3, 4, 5); // returns 120
- Object destructuring: You can pass an object as an argument and destructure its properties to obtain the parameter values. This can make the code more readable and easier to understand. For example:
function area({ width, height }) { return width * height; } area({ width: 10, height: 20 }); // returns 200
In this case, the argument passed to the area
function is an object with properties width
and height
. The function uses destructuring to extract these values and calculate the area.
- Object spread: You can use the object spread syntax (
...
) to pass an object as an argument and spread its properties into individual parameters. For example:
function area({ width, height }) { return width * height; } const dimensions = { width: 10, height: 20 }; area({ ...dimensions }); // returns 200
In this case, the object dimensions
is passed as an argument to the area
function. The spread syntax is used to spread the object properties into individual parameters for the area
function.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments