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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Build modern and responsive webpages

The Modern Web

Build modern and responsive webpages Try Front-end

The Modern Web will guide you through the most important tools of device-agnostic web development, including HTML5, CSS3, and JavaScript. The plain-english explanations and practical examples emphasize the techniques, principles, and practices that you'll need to easily transcend individual browser quirks and stay relevant as these technologies are updated.

$ Check price
34.9534.95Amazon 4.5 logo(30+ reviews)

More Front-end resources

Ad