Need help in understanding following JavaScript code [duplicate] - javascript

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 5 years ago.
I don't understand how the expression of return of following code works.
function reverse3(array) {
return array.map((element, idx, arr) => arr[(arr.length - 1) - idx]);
}
This function gives reverse of array parsed to the function. But I actually don't understand and didn't get idea after googling for hours about how that statement in map() function works. Please give me explanation about for what those three variables stands and what that => operator does. THANKS in advance!!

Related

Why does every() method work only one line? [duplicate]

This question already has answers here:
Why doesn't my arrow function return a value?
(1 answer)
Curly Brackets in Arrow Functions
(3 answers)
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed 1 year ago.
The following code works:
let check = checkingImages.every((item)=>item.classList.contains('matched'))
But this one doesn't:
let check = checkingImages.every((item)=>{
item.classList.contains('matched')
})
Can someone explain what's the reason for the second code not working, please?

Javascript map() method and arrow function [duplicate]

This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
How is () => {...} different from () => [duplicate]
(4 answers)
Arrow function without curly braces
(9 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 2 years ago.
This probably suits other methods as well but the one i'm using at the moment is map().
How come:
const singleFruit = fruits.map((fruit) => fruit.toUpperCase());
console.log(singleFruit);
returns the array the correct way, with everything in uppercase, when:
const singleFruit = fruits.map((fruit) => {
fruit.toUpperCase();
});
console.log(singleFruit);
gives me an array but with my assigned fruits are now undefined. I can solve this by adding return before "fruit.toUpperCase();" I thought that the second code was the exact same as the first one but without the curly braces.
Thanks!

Anonymous function execution in JavaScript [duplicate]

This question already has answers here:
Explain the encapsulated anonymous function syntax
(10 answers)
Why are parentheses required around JavaScript IIFE? [duplicate]
(3 answers)
Closed 2 years ago.
Why if I have
function(){...}()
does not work while when I put inside brackets like
(function(){...}())
it works?

JavaScript object properties copy syntax [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
What do {curly braces} around javascript variable name mean [duplicate]
(1 answer)
Closed 4 years ago.
I was reading some JavaScript code and saw a line of code similar to this.
const {name, password, email} = user;
I tried searching around but could not figure out what this syntax is called so I am not able find the documentation. Is this a new JavaScript syntax? What is it called? What does it do exactly?

how does arrow function with spread operator work [duplicate]

This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
How does the spread syntax in ES6 JS work as a function argument?
(1 answer)
What are these three dots in React doing?
(23 answers)
Closed 5 years ago.
How does this work:
const invert = fn => (...args) => -fn(...args);
Specifically, what is happening with (...args). It behaves as if it is separating the arguments from the function after the first fat arrow, but I suspect there is a better, more specific, explanation. Thanks.

Categories