This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed 1 year ago.
When I log this to the console it shows up as undefined. However, when I remove the braces in the function it logs the desired result. I'm confused -- aren't we always supposed to include curly braces in a function? Thank you all.
const shoutGreetings = arr => {
arr.map(word => word.toUpperCase() + '!')
};
const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
console.log(shoutGreetings(greetings))
Return the result of your map function:
return arr.map(...
Related
This question already has answers here:
Closure in JavaScript - whats wrong?
(7 answers)
"add" function that works with different combinations of chaining/arguments
(4 answers)
Puzzle: JS Function that returns itself until there are no arguments
(5 answers)
Variadic curried sum function
(19 answers)
Closed 1 year ago.
I just started learning closures in JavaScript and I'm trying to understand a basic problem.
For example, I'm trying to implement the sum method: sum(1)(4)(5)
const sum = (a) => {
return (b) => {
if(typeof b === 'number')
return sum(a+b)
return a;
}
}
When I call: console.log(sum(1)(4)(5)()) it works perfect and return 10. However, if I call console.log(sum(1)(4)(5)), it returns [Function (anonymous)]. Why?
Thanks
Every time you call your function like:
sum(1)(10)
you are returning the inner function:
(b) => {
if(typeof b === 'number')
return sum(a+b)
return a;
}
Because type of b === 'number' and you are returning sum(a+b) that calls
again the function sum and returns again the inner function. Thats why when you finally put the last parenthesis like:
sum(1)(10)()
The inner function will execute and type of b in this case is different from number and will return 'a' that already contains the sum of the values.
This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
Arrow function without curly braces
(9 answers)
Closed 2 years ago.
Imagine I have an array containing movie objects such that
const movies: Movie[] = [ movie1, movie2, movie3, movie4 ];
I want to select a movie, for instance I have chosen movie2, then I want movie2 to be removed.
The following code works, Imagine m is the selected movie:
movies = movies.filter( m => m !== movie );
I want to understand how the arrow function works and tried the following code but it did not work.
movies = movies.filter( m => {
m !== movie;
});
What is the difference between those two codes?
The difference is, that the short-hand version implicitly returns the result. The long version from you is missing the return statements, like:
movies = movies.filter( m => {
return m !== movie;
});
This question already has answers here:
What does `return` keyword mean inside `forEach` function? [duplicate]
(2 answers)
Short circuit Array.forEach like calling break
(30 answers)
Closed 4 years ago.
function checkMagazine(magazine, note) {
var map = new Map();
var noteAr = note.split(" ");
var magazineAr = magazine.split(" ")
noteAr.forEach((note) => {
if (map.has(note)) {
map.set(note, map.get(note) + 1)
} else {
map.set(note)
}
});
magazineAr.forEach((word) => {
if (!map.has(word)) {
return "No"
}
});
return "Yes"
}
I'm checking to see if each word of a note is contained in the magazine by first hashing the values of the note and checking them in magazineAr. If the note word does not exist in the hash, then I am return "NOing. However, I keep getting a return "YES" in console.
I've watched it in debugger mode and I see it enter the statement where it hits 'return No' but then it just keeps on going with the forEach loop. I've even tried return false but same thing.
A forEach callback ignores the value that's returned - no matter what it is, every item in the array will be called with the forEach. For what you're trying to do, you should probably use every instead, which checks whether all items in the array fulfill the condition:
const hasEveryWord = magazineAr.every(word => map.has(word));
return hasEveryWord
? "Yes"
: "No";
This question already has answers here:
Arrow function without curly braces
(9 answers)
Curly Brackets in Arrow Functions
(3 answers)
Closed 4 years ago.
What's the difference between these two in javascript? what does it mean in scripting?
const Test1 = () => {
console.log('test1')
}
const Test2 = () => (
console.log('test2')
)
The "basic" form is with curly braces, just like regular functions:
() => {
...
}
However, arrow functions allow one special case shorthand:
() => plain expression
If you don't use curly braces, you may use one plain expression instead, with an implicit return. I.e. these two are equivalent:
() => { return 42; }
() => 42
So your version using parentheses counts as the single expression version and the return value of console.log will be returned (which is undefined either way though), whereas it won't on the version using curly braces.
Second example used to simplify returning of function, but in this case you can use only one expression, so you cannot write large of code. Try to run this example to better understand:
const Test1 = () => {
'test1'
}
console.log(Test1())
const Test2 = () => ( test = 'test4')
console.log(Test2())
Also this declaration method uses to simplify returning objects:
const Test3 = () => ({ a: 1, b: 2 });
console.log(Test3());
This question already has answers here:
Difference between ES6 object method assignment: a, 'a', and ['a']?
(2 answers)
Closed 6 years ago.
I've found some wild code on the web i don't understand:
return Object.assign({}, state, {
[action.subreddit]: posts(state[action.subreddit], action)
})
What is [action.subreddit] doing? I thought that object keys had to be strings but this appears to be an array?
I'm hoping to understand mechanically how this code works.
thank you!
That's not an array as key, it's the es6 way to use a variable (/ a computed property) as the key.
Consider this:
var a = "foo";
function getKey() {
return "myKey";
}
var obj = {
[a] : "bar",
[getKey()] : "baz"
};
console.log(obj.foo); // bar
console.log(obj.myKey) // baz
So [action.subreddit] just sets the key's name to whatever value action.subreddit is holding.