This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 7 years ago.
Code example:
function unusedDigits(...args){
return [0,1,2,3,4,5,6,7,8,9].filter(o => args.join("").indexOf(o) === -1).join("")
}
Everything is clear here. Exept =>. What does this mean in javascript?
This is a ES6 arrow function which is a short syntax for function expression. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
So:
// ES5
var selected = [0,1,2,3,4,5,6,7,8,9].filter(function (o) {
return args.join("").indexOf(o) === -1;
});
// ES6
var selected = [0,1,2,3,4,5,6,7,8,9].filter(o => args.join("").indexOf(o) === -1);
Related
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(...
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:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 3 years ago.
I ran into this error of "Cannot read property 'localeCompare' of undefined"
when I was trying to convert a string function of compare(String, String) to String.compare(String)
let compare = (y, x) => y.localeCompare(x) == 0 ? true : false; //This works
let gender = x => compare("male", x) || compare("female", x); //This works
String.prototype.compareTruthy = (x) => {
this.localeCompare(x) == 0 ? true : false;
}
"male".compareTruthy("male") //This does not work, why?
I wanted to re-use this compareTruthy function for any other string comparisons I might have later on.
What am I missing in my understanding?
Arrow function expressions don't have the context of the current object. Read this page.
You need to use:
String.prototype.compareTruthy = function(x) {
.....
}
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
function s(e) {
return e.toString();
}
var a = s(3); // works
var b = 3.toString(); // error
For example, set var a to the return value of s() that returns the first argument.toString(), but you can't set var b to 3.toString()
Javascript is expecting number(s) after the decimal, you can still do this, but you need to put your number in parenthesis:
(3).toString() = "3"
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 7 years ago.
What does => mean in javascript? I see it here
var N = 50
function asyncFunc (cb) {
setTimeout(() => cb(Math.random()), 100)
}
function loop (max, results, done) {
// Recursion base-case
if (results.length >= max) return done(results)
asyncFunc((res) => {
results.push(res)
loop(max, results, done)
})
}
let randomNumbers = []
loop(N, randomNumbers, function (results) {
console.log(results)
})
It appears twice in setTimeout() and asyncFunc().
This is called the 'fat arrow' it will be coming to JavaScript with ES6 but it has also be implemented in languages that transpile to javascript like coffescript. You can read about it more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
It's an "arrow function" - a new (in ES6 aka ES1015) short hand notation for functions that's particularly useful for callbacks.