What are javascript “...array” parameters? [duplicate] - javascript

This question already has answers here:
Spread Syntax ES6
(8 answers)
Closed 4 years ago.
I just started learn Javascript, I'm confused about the function parameter ...[1,2], why the function parameter like this.
function compare(a, b) {
return a - b;
}
let result = compare(...[1,2]);
console.log(result);

It's a new Es6 functionality called the spread operator, it's great for calling functions (without apply), converting arguments or NodeList to Arrays, array manipulations, and even when using the Math functions.
https://davidwalsh.name/spread-operator

Related

Pass to method object values as arguments [duplicate]

This question already has answers here:
Passing an array as a function parameter in JavaScript
(12 answers)
Closed 2 years ago.
I have next code:
someFunct(Object.values(Obj));
What I need is to pass object values in to 'someFunct' as separate argument, not as array.
You can use the spreading operator to spread the values:
someFunct(...Object.values(Obj));
You should use spread operator ...
someFunct(...Object.values(Obj));
Note: This method may cause some problems in code because Object.values() is not guaranteed to have same order. The better option is to pass and object and then destructure it.

Use all function arguments without having to enumerate them [duplicate]

This question already has answers here:
Usage of rest parameter and spread operator in javascript
(6 answers)
Closed 4 years ago.
I have functionWithManyArguments(arg1,...,arg15).
Can I log all arguments without having to enumerate them all?
Yes,
You could do something like:-
function foo(...args) {
console.log(...args);
}
foo(1, 2, "a")
...args are rest parameters.

Array function of JavaScript && Rest Parameter? [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What is the meaning of "...args" (three dots) in a function definition?
(6 answers)
Closed 5 years ago.
I was reading the JavaScript Docs I found the Array function section one Example that I could not understand properly. Because I am learning Programming.
function multiply(multiplier, ...theArgs){
return theArgs.map(x=> multiplier*x);
}
var arr = multiply(2,1,2,3);
console.log(arr);
Above example is giving
2,4,6
My first question why is used 3 dots(...) before function argument name (...theArgs) for? And How did it calculate 2,4,6?
And the second Question is about rest parameter. What does the rest parameter use in Array Function?
I would be glad if anyone can give me some real-life object example to understand this Problem

When is the "=>" syntax in relation to defining a function in javascript and what is its significance? [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 6 years ago.
I'm just learning node.js so Im not sure if this a node.js question or a javascript question but Im not sure what this syntax is/is saying. I don't believe it's being to compare less than or greater than values as I've seen it used like so in node.js apps
router.use('/intro', (req, res) => {
});
What is the "=>" saying / what is it's significance?
This is a javascript question, it is a new way to write functions...
This is simply es6 arrow function syntax.
ES5
var a = array.map(function(s){ return s.length });
ES6
var b = array.map( s => s.length );
Your code in ES5:
router.use('/intro', function(req, res) {
});
Note:
Differences with Lexical this
Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an "object method", etc.). This proved to be annoying with an object-oriented style of programming.
See these docs for more info
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Javascript .filter() function accepting things other than a function? [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 6 years ago.
I have found an example of somebody using the .filter() function without passing it a function as a parameter. I am puzzled as to how this works, here is an example of the code:
var integers = [1,2,3,4,5,6,7,8,9,10];
var even = integers.filter(int => int % 2 === 0);
console.log(even); // [2,4,6,8,10]
I am confused because I thought that filter must take a function as an argument, but instead is comparing "int" against "int % 2 === 0".
How is this happening? why does "int" not have to be declared and why can filter accept something that isn't a function?
Thanks!
The parameter of the example IS a function, arrow function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
It's basically a shorthand syntax for declaring a function which returns the first statement;
That's a function defined using the "fat arrow" notation (=>). It's also called a lambda and is new in ES6.

Categories

Resources