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

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.

Related

Do I have to address JavaScript function variables in order? How to do it? [duplicate]

This question already has answers here:
Skip arguments in a JavaScript function
(9 answers)
Closed 9 months ago.
How do I send one or more variables in a JavaScript function without sending null values to unused arguments?
I have a function:
function setProjectParameters(trigger, pp_ts, pp_t, pp_bs, pp_dg, pp_ll){alert("the rest of the code is here doing something");}
Sometimes I want to call this function and only send a value to pp_bs, now I do it like this:
setProjectParameters(null,null,null,"8");
Can I do it something like this?: (It doesn't work so I guess not...)
setProjectParameters(pp_ts="44");
What is the proper way to do it?
The easiest I think is to have the parameters in the function as an object and have defaults for them if you need to. Then you can target a specific parameter.
function setProjectParameters({trigger = 'if you need defaults add them like this', pp_ts, pp_t, pp_bs, pp_dg, pp_ll}){alert("the rest of the code is here doing something");}
Then use the function like this
setProjectParameters({pp_ts="44"});

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.

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

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

Categories