Arguments

Functions can be set up so that they accept arguments (the stuff between the parentheses). Arguments are values passed into a function that help determine the behavior of the function. For instance, the argument for console.log is whatever text you want displayed in the console. A regular expression’s exec function accepts a string argument and tries to find matches for the regular expression in the string.

Arguments let you make your functions more flexible. For instance, our countToTen example is not flexible at all. What if we want to count to only 7, or all the way to 15? We can let countToTen count to any number by using an argument (although we’d better change the name of the function because it will be capable of counting to ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.