Javascript map() method and arrow function [duplicate] - javascript

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!

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?

Add isEmpty to Array prototype [duplicate]

This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
I want to add a property to the object Array. I've been trying the following:
Array.prototype.isEmpty = array => (array || []).length <= 0 // only works if I use array.isEmpty(array) which isn't exactly what I want.
Array.prototype.isEmpty = (() => (this || []).length <= 0)() // always returns true since this is always undefined.
Is there a way to add this in JavaScript?

arrow function inside an object literal [duplicate]

This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
i have this object literal
let p = {
name : 'Amir',
say: () => {
console.log(this.name)
console.log(this)
}
}
and I want the say function works using this
p.say();
but the arrow function obviously gets the window object as 'this'. I know I could use a regular function for 'say' instead of arrow and it will work fine.
BUT
I would like to ONLY change the call to say function to make it work, but the binding won't work.
I mean something like p.say.bind(p)() or p.say.call(p) aint gonna work as desired.
Is it possible to change the call to function ONLY and not the say function?

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.

Need help in understanding following JavaScript code [duplicate]

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

Categories