This question already has answers here:
Curly Brackets in Arrow Functions
(3 answers)
Why doesn't my arrow function return a value?
(1 answer)
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed last month.
I've summerized my problem in an example below, the problems is, if I use in an object an arrow function with only one item (23) in the function (as below), I've to not to use curly brackets for it to work. Why is so? I thought it was optional not to use curly brackets when in the function there is only one item. Thanks
Doesn't work with curly brackets, returns undefined
obj = { y: () => {23} }
console.log(obj.y());
Works without curly brackets, returns 23
obj = { x: () => 23 }
console.log(obj.x());`
"to work with or without the curly brackets"
Related
This question already has an answer here:
Why doesn't my arrow function return a value?
(1 answer)
Closed 2 years ago.
Below is my code for move all characters forward by one for a given string.
ex. abc => bcd
const moveCharForward = (str) => {
str
.split('')
.map(char => String.fromCharCode(char.charCodeAt(0) + 1)).join('');
}
console.log(moveCharForward('abcd'));
when I called the method it throws undefined.
I modified the code by removing the curly brackets like below.
const moveCharForward = (str) =>
str
.split('')
.map(char => String.fromCharCode(char.charCodeAt(0) + 1)).join('');
console.log(moveCharForward('abcd')); //working correctly
Now when I called the method its working correctly.
I want to know why throws undefined when Im adding method implemetation inside the curly brackets?
When using arrow functions like this, if you don't use curly braces, then JavaScript implicitly returns the value following the arrow. However, if you do use curly braces, then JavaScript expects a block of code, in which there must be a return statement in order to return a value
When you add the curly braces you need the return keyword. Without curly braces the return is implied
This question already has answers here:
What does the arrow function with a () after means? [duplicate]
(3 answers)
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 4 years ago.
I just came across this code:
routes: routes.map(route => ({
Notice the arrow function being followed by a closure. Why are closures used here?
see this repo for reference: https://github.com/prograhammer/vue-pizza/blob/master/src/http/router.js#L33
It returns an object. The parenthesis is to denote that this is an object not a block.
() => {return {hello: 'world' } } === () => ({hello: 'world'})
This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 5 years ago.
Is it possible to write the code below in one line (without return keyword)?
elements.map(element => {
return {...element, selected: false};
})
Yes, by using the concise arrow form, enclosing the object initializer in ():
elements.map(element => ({...element, selected: false}));
// ---------------------^-----------------------------^
You need the () because otherwise the { of the object initializer is read as the { as the beginning of a function body. The ( instead makes it an expression body with implied return.
This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 6 years ago.
Im using the shortened ES6 arrow function notation to write my function body like this
const funcName = (...args) => something
I'm not even using a single curly bracket since my function body has just 1 return statement.
But say I want to return an object from this function, I would have to do:
const funcName = (...args) => {key:val}
The problem here is babel is assuming that the stuff inside the curly brackets is a function body - not an object - how to make it think that it is indeed an object??
Embrace it with brackets
const funcName = (...args) => ({key:val})
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.