Why passing extra arguments to a function works in JavaScript? [duplicate] - javascript

This question already has answers here:
What happens if I call a function with more arguments than it is defined to accept?
(5 answers)
Closed 5 years ago.
Let's say I've defined a function :
function myAwesomeFunction(x){
console.log(x);
}
now, what I expect is, if I'll call it like this: myAwesomeFunction(1) or myAwesomeFunction('lol') or myAwesomeFunction('whatever'), it'll work and it does.
but how does it work, even when I pass extra arguments to the function and simply ignores all arguments except the first one :
myAwesomeFunction('why so', 'serious?')
we don't even have any optional arguments in the above function?(i.e, like (x, y=''))
function myAwesomeFunction(x){
console.log(x);
}
myAwesomeFunction(1);
myAwesomeFunction('lol');
myAwesomeFunction('whatever');
myAwesomeFunction('why so', 'serious?')
myAwesomeFunction('why', 'so', 'serious?')

You can call a Javascript function with any number of parameters, regardless of the function's definition.
Any named parameters that weren't passed will be undefined.
Javascript treats your parameters as an array. More specifically it's the arguments array, Named parameters in function declarations are just pointers to members of arguments. More info here

When you defined the function, you gave it one parameter x
The language is designed to look for a single argument for each parameter, and ignore the others.
However, you are able to access the other arguments via the special arguments object.
Try typing console.log(arguments) within your function body and seeing what happens...

Related

How to read function expression syntax in javascript? [duplicate]

This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 1 year ago.
function [name]([param1[, param2[, ..., paramN]]]) {
statements
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function
I am confused what and how to read the above function input values, it is a infinite params input, but don't understand the used of brackets[], why put those.
Square brackets represent optional
So it means, that [name] is optional, function declared without name become an anonymous function.
[param1[, param2[,... this means that the parameters are optional, however because they are shown inside previous parameter (all closing ] brackets are at the end of the list) means that you can't have param2, without param1, or param3 without param2 and param1 and so on.

How to figure out the number of parameters in the passed callback function? [duplicate]

This question already has answers here:
Get a function's arity
(5 answers)
Closed 3 years ago.
When using a 3rd party library I often find an optional parameter in the callback.
For example, in Mocha when the callback done parameter exists, it waits for done to be invoked before moving on to another test case.
someFunction(function(done) { /** code **/ })
someFunction(function() { /** this behaves differently than above **/ })
How can I achieve the same behavior?
You can check the length attribute of the function object
console.log((()=>42).length); // 0
console.log(((x,y)=>42).length); // 2
note however that you cannot be sure of exactly how many the function is going to access, because it's also possible to use arguments inside Javascript non-arrow functions and "rest" parameters inside arrows (that are not counted in .length attribute).

what is this meaning "assert(value[, message])" [duplicate]

This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 5 years ago.
This is a javascript function assert(value[, message]).
I want to know what is difference between assert(value[, message]) and assert(value, message).
The square brackets are used when writing function descriptions to indicate that the parameters are optional.
In the case of assert(value[, message]) the first parameter, value, is required. If you try to call the function without it -- assert(); -- the function will not work or will throw an error. The second parameter, message, is optional. You can call the function with just the first parameter -- assert(value); -- and it will work correctly.
If the function is shown as assert(value, message) then both parameters are required and they both must be given in order for the function to execute as expected or without throwing an error.

what does higher-order function mean in javascript? [duplicate]

This question already has answers here:
Higher order functions - Javascript
(2 answers)
Closed 9 years ago.
javascript book "eloquent javascript"
function negate(func) {
return function(x) {
return !func(x);
};
}
var isNotNaN = negate(isNaN);
show(isNotNaN(NaN));
someone explain it and as title of question says what is higher-order function, what does this code do ?
When most people think of functions they accept objects or values as parameters and similarly return an object or value, such as function addTwoNumbers(int x, int y).
In mathematics and computer science, a "higher-order function" is just like any other function, except that in addition to arguments that are values it can also accept a function as an argument.
...that's all a higher-order function is, really :)
In the example you posted, negate is a higher-order function because it has a parameter func which is a function (or rather, assigned to a function).
negate goes further: it doesn't merely call func and negate its result, instead it returns an anonymous function (that's the inner return function(x) bit).
So the isNotNaN variable then has the type (and value) of that earlier anonymous function.
Higher-order function is a function that:
Take one or more functions as input.
Give another function as output.
What does your code do? it negates the function isNan (to isNotNan). It accept a function (isNan), and then output the negation (isNotNan). It's just that simple.

difference between javascript function declaration [duplicate]

This question already has an answer here:
Anonymous function vs normal function
(1 answer)
Closed 9 years ago.
I know this type of question gets asked a lot, but I haven't seen any question with this type of declaration
(function(){
myFuncName=function(myVar){
// some logic
};
}());
how does that differ from
function myFuncName(myVar){
// some logic
}
The first one is an anonymous function and there is no way you can reference to and call it later on, so you just executes instantly ( ) once it had been created!
(function(){
alert(1)
}())
The second one is a reference function that you can call it anytime later. It wont gets executed unless you call it explicitly
What your doing is creating an anonymous function that also has a closure in it. Read more about closures here.
Basically a closure means that you have declared a function inside of another function. Which will let you access local variables after the first function exits.
Normally you would not be able to do this since they would be out of scope.
As for the other part. You can find a very useful guide to what's happening here Why do you need to invoke an anonymous function on the same line?.
To sum it up though, you have created an anonymous self-invoking function expression.
The self-invoking comes from the fact that () immediately follows the function expression.
Here's a link which explains the the function declarations in javascript.

Categories