02. Arguments and Parameters

Using function arguments

If you want to pass in values as part of your call to your function, you may place them between the parentheses. With multiple arguments, use commas.

// Write greeting
function writeGreeting(greeting) {
  document.write(greeting);
}
 
// Function that gets the area of a square
function getArea(width, height) {
  return width * height;
} 

Parameter Rules

JavaScript, unlike other strongly typed languages, does not check specific data types when a parameter is passed onto the function. This means you can't restrict someone from passing in a String input when you want a numerical value.

Furthermore, JavaScript does not check for the number of arguments received. This means that you can pass as many or as little variables within a function as you want.

In the case where you have missing arguments, the values are set to undefined. On the other hand, if you have too many arguments, the extraneous arguments can only be accessed wtih the arguments object.

Arguments vs. Parameters

Parameters are the variables used before a function is called. In the case of the function getArea(width, height) above, the width and height variables are the parameters.

Once we call the function, the parameters are turned into arguments.

getArea(5,6); // parameters 5 and 6

Arguments object

The argumentsobject contains information on the arguments passed into a function. You can use this variable from within a function to access elements you may otherwise not have.

With this object, you can access all in an array-like fashion. Furthermore, you can find the number of arguments by accessing its length property.

// Return minimum value
function findMin() {
  var i, min = 0;
  for (i = 0; i < arguments.length; i++) {
    if (arguments[i] < min) {
      min = arguments[i];
    }
  }
  return min;
}

Primitive types are passed by Value, Objects by reference

Arguments are passed by value, meaning the actual value is passed in. Objects, on the other hand, pass in a reference to their address. This means that any changes on that object from within the function will retain those changes from outside.

Build modern and responsive webpages

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad