What does the "=>" operator do in Javascript [duplicate] - javascript

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 the "=>" operator do in Javascript? This is probably a very basic question, but it is ungoogle-able. Does this operator have a name (to help me find it in references)? It seems to be some kind of remapping function. An example of where I've found it:
let maxLen = Math.max.apply(null, arrays.map(v => v.length)), out = [];
for finding the longest array held in arrays.

They are called arrow functions. It's an alternate way of defining a function introduced in Ecmascript 6.
An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.
You example is equivalent to:
let maxLen = Math.max.apply(null, arrays.map(function(v){ return v.length; })), out = [];
There is an in depth explanation of arrow functions here, which explains them far better than I can.

This is a ES6 shortcut, it means:
arrays.map(function ( v ) {
return v.length;
});
With the arrow you can execute a statement, like in your example, or a code block:
arrays.map(v => {
// do something long with v
return theValue;
});

Related

Arrow function behave differently inside object from traditional Javascript function [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
Closed 4 years ago.
In ES5, other properties in object can be accessed by using this keyword.
Same behavior but can't able to access other property in object by using this in arrow function (ES6)
Kindly run attached code snippet see the output
var person={
"firstName":"PraveenRaj",
"lastName":"D",
"getFullName": function() { return this.firstName+" "+this.lastName}
}
var person1={
"firstName":"PraveenRaj",
"lastName":"D",
"getFullName": () => this.firstName+" "+this.lastName
}
console.log(person.getFullName());
console.log(person1.getFullName());
Like all JavaScript variables, both the object name (which could be a normal variable) and the property name are case sensitive. You access the properties of an object with a simple dot-notation.
Following is the syntax for accessing Object Properties.
objectName.propertyName
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
Credits: Docs, Reference Answer

When is the "=>" syntax in relation to defining a function in javascript and what is its significance? [duplicate]

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'm just learning node.js so Im not sure if this a node.js question or a javascript question but Im not sure what this syntax is/is saying. I don't believe it's being to compare less than or greater than values as I've seen it used like so in node.js apps
router.use('/intro', (req, res) => {
});
What is the "=>" saying / what is it's significance?
This is a javascript question, it is a new way to write functions...
This is simply es6 arrow function syntax.
ES5
var a = array.map(function(s){ return s.length });
ES6
var b = array.map( s => s.length );
Your code in ES5:
router.use('/intro', function(req, res) {
});
Note:
Differences with Lexical this
Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an "object method", etc.). This proved to be annoying with an object-oriented style of programming.
See these docs for more info
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Javascript .filter() function accepting things other than a function? [duplicate]

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.

Spring react - operator => [duplicate]

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.
I am just learning Spring react, I don't know Javascript very well. I got to code:
componentDidMount: function () {
client({method: 'GET', path: '/api/employees'}).done(response => {
this.setState({employees: response.entity._embedded.employees});
});
There is written:
componentDidMount is the API invoked after React renders a component
in the DOM.
I was looking for what Javascript operator => means. But don't found anything.
This is an arrow function.
An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.
You can see a simple example:
var vec = ['a', 'ab', 'abc'];
var test = vec.map(i => i.length);
alert(test); // 1,2,3

What is the meaning of "=>" when used in an argument to array.map()? [duplicate]

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.
In a tutorial, the presenter used an unfamiliar syntax in an array.map function expression like this:
.map(x => x.trim());
map() takes a callback function, suggesting thet this expression is creating a function. Searching for "=>" here and generally on google returned no recognizable hits. Searching on symbols can often be difficult, but I can't think of a good way to learn what this is doing.
What is the meaning of this expression?
It's an arrow function:
An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.
From the doc, these two are equivalent:
a.map(function(s){ return s.length });
a.map( s => s.length );
This syntax was added to Javascript in ES 2015.
These are lambda functions, ECMA defines them as arrow functions.
Arrow Functions
What you see is an arrow function which is only valid syntax in ES6.
It is almost the same as saying .map(function(x) { return x.trim(); })

Categories

Resources