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.

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Build modern and responsive webpages

High Performance Web Sites

Build modern and responsive webpages Try Front-end

Want your web site to display more quickly? This book presents 14 specific rules that will cut 25% to 50% off response time when users request a page. Author Steve Souders, in his job as Chief Performance Yahoo!, collected these best practices while optimizing some of the most-visited pages on the Web.

$ Check price
29.9929.99Amazon 4.5 logo(63+ reviews)

More Front-end resources

Ad