This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
The last few articles have learned a few amazing little “bugs” that JavaScript isn’t perfect but don’t affect how it works:
- JavaScript learning-for-loop
- Also run with imperfect JavaScript – autosemicolon (;) The problem,
- JavaScript imperfect can also run – array object addition problem,
- JavaScript not perfect arrow function correlation (2)
- JavaScript is not perfect
This article is about learning JavaScript function arguments – optional arguments
Arguments to a JavaScript function
arg1, arg2, ... argN
Functions use zero or more names as formal parameter names. Each must be a string or comma-separated list of strings that conform to valid JavaScript identifier rules, such as “x”, “theValue”, or “a,b”.
Use function to define a common function in JavaScript:
function myFunc(arg1){
return arg1 * 2
}
console.log(myFunc(10512.'Arg2 is a string'.'Arg2 and arg3 will be ignored')
/ / - > 21024
Copy the code
As you can see in the code above, we defined the function with one argument arg1, but passed multiple arguments arg2 and arg3. The JavaScript language ignores the extra arguments we passed and evaluates the expression…
JavaScript function Optional argument
JavaScript has relatively loose parameters for functions. If we pass more parameters than are used in the function body, JavaScript will ignore the parameters passed.
Similarly, if you pass too few arguments to a function and the function body needs to be used, JavaScript assigns it undefined..
Here’s a minor problem with JavaScript: when we pass the wrong number of arguments to a function, we don’t get an error, and we don’t get effective feedback…
thinking
Just like the optional parameters of a function, we always work with one or more alternatives and backups of data in case of loss due to unpredictable factors.