Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Function() > Passing Parameters to a Function

Passing Parameters to a Function

Parameters are vehicles for passing values into the scope of a function when it is invoked. Below, as we invoke addFunction(), since we have predefined it to take two parameters, two added values become available within its scope.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var addFunction = function(number1, number2) {
   var sum = number1 + number2;
   return sum;
}

console.log(addFunction(3, 3)); // logs 6

</script></body></html>

Notes

  • In contrast to some other programming languages, it is perfectly legal in JavaScript to omit parameters even if the function has been defined to accept these arguments. The missing parameters are simply given the value of undefined. Of course, by leaving out values for the parameters, the function might not work properly.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint