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

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.

Related

_ as a variable in JavaScript function [duplicate]

This question already has an answer here:
Understanding arrow function parameters
(1 answer)
Closed 1 year ago.
I came across this function:
const rotatedTetro = matrix.map((_, index)=> matrix.map(col => col[index]))
What does the _ passed in mean?
There’s nothing special about underscore as a variable name. It’s just a convention often used to indicate that the argument won’t be used, but it needs to be present because you need the args that follow it.
It’s a way of indicating the code is “skipping over” that argument.

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.

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

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...

Javascript: What is function with two parenthesis brackets ()()? [duplicate]

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
What is 'Currying'?
(23 answers)
Closed 5 years ago.
I recall somewhere seeing a function with two parenthesis brackets ()() like:
function add_numbers(number1)(number2)
What do you call such a function and what’s its usage?
Thank you in advance and will be sure to vote up/accept answer
It is called function currying. The first bracket returns another function(lets call it: "myCustomFunc"). The 2nd bracket actually passes the 2nd value (number2) to the myCustomFunc.
Going off of this answer, the add_numbers function takes one argument (number1) and returns a function, which is then called with argument number2.

Categories