Parameter defaults

These allow us to put in default values if we do not have something for that position when calling a function. This may appear as follows:

const defParams = function(arg1, arg2=null, arg3=10) {
if(!arg2 ) {
console.log('nothing was passed in or we passed in a falsy value');
}
const pow = arg3;
if( typeof arg1 === 'number' ) {
return arg1 ** pow;
} else {
throw new TypeError('argument 1 was not a number!');
}
}

One thing to note with parameter defaults is that once we start to utilize defaults in the chain of arguments, we can't stop using defaults. In the preceding example, if we gave argument 2 a default, we would have to give argument 3 a default, even if we just pass undefined or null to it. Again, this helps with the clarity of code and making sure that we no longer have to create default cases where we are looking at the arguments of the array.

A lot of code still utilizes the arguments section of a function. There are even other properties of a function that we can get at such as the caller. If we are in strict mode, a lot of this behavior will break. Strict mode is a way to not allow access to certain behaviors in the JavaScript engine. A good description of this can be found at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode. In addition to this, we should no longer be using the arguments section of the function since we have plenty of helpful alternatives with the new standard.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.244.228