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) {
// 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?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

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