0

Now that we have a better understanding of JavaScript Functions, we can now provide optional arguments to those functions. Why would you want an optional argument in a function you might ask? Well, there are many different reasons, but a good example is in the actual structure of JavaScript. For example, the string method of split has a required argument and an optional one. The required argument is the separator, which is what character(s) you will use to divide the string. The optional argument is limit where you specify how many times you want to split the string. So, if I only wanted to split the string to the first instance of the separator, I would just provide a 1. Example please!

Example

message = "The quick brown fox jumped over the lazy dog.";

console.log(message.split(""));

Result

["T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "e", "d", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", " ", "d", "o", "g", "."]

Now, we put in the optional argument of 1.

Example

message = "The quick brown fox jumped over the lazy dog."; console.log(message.split("",1));

Result

["T"]

JavaScript Tutorial and Reference

How Optional Arguments Work

That is cool and awesome, but how exactly do we create functions that can do this? If we create a function with 2 arguments and only provide 1, JavaScript will scream errors at us. That is very true, but there is a neat little way to handle this before the errors occur. First, JavaScript doesn't yell at you if you have multiple arguments, but don't specify them when you call the function. JavaScript only screams murder when you leave off an argument that sets a variable inside the function that is called. For example...

Example function printMessage(times, message)

{

for(var a = 0; a < times; a++)

{

console.log(times);

}

}

printMessage(4);

Result 4

4

4

4

See JavaScript doesn't care that you left off the message argument because it didn't need it anywhere in the function. Now, if you were to use message in the function, JavaScript would throw and error.

Creating Optional Arguments

Let's get back to actually creating optional arguments in our functions. You will notice that the easiest and best way to create optional arguments is to put them at the end of the argument list. Sometimes you might want two optional arguments where only one has to be required. However, that is much more complicated and outside the scope of this simple lesson.

Example function printMessage(times, message)

{

if(message === undefined)

{

message = "No Default Message";

}

for(var a = 0; a < times; a++)

{

console.log(message);

}

}

printMessage(2);

Result No Default Message

No Default Message

See how simple it really is? All we did was create an if statement to check if the function call specified an argument message by seeing if it was equal to undefined. Since it was undefined, we went ahead and set our own value to the message variable. So when JavaScript finally saw us using the message variable, it didn't think twice of wondering why it wasn't specified in the function call.





Source by Jared B Drake

Post a Comment

 
Top