Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean equal and greater-than sign (=>) in Javascript? [duplicate]

In Meteor Whatsapp example project is file where "=>" is used, but my WebStorm IDE detect it as error. I can't find any docs about this syntax.

chats.forEach( chat => {
  let message = Messages.findOne({ chatId: { $exists: false } });
  chat.lastMessage = message;
  let chatId = Chats.insert(chat);
  Messages.update(message._id, { $set: { chatId: chatId } })
});

GitHub repository for bootstrap.js file is here

What is "=>" ?

like image 685
Benny7500 Avatar asked Oct 27 '15 18:10

Benny7500


People also ask

What does the => sign mean in JavaScript?

It's an arrow function, newly defined in ES6. An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.

Why does JavaScript use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

Is == and === same in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What does double equal sign mean in JavaScript?

Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other.


2 Answers

I was actually about to downvote this question, but googling the answer proved surprisingly difficult if you don't already know what its called. As you can see in the links in the comments, that's a fat arrow function (sometimes referred to as just an arrow function).

There are some confusing aspects of arrow functions, so I'll hit some highlights:

Normal functions have a this pointer set depending on the context: functions called with new have it set to the newly-created object, functions called as methods have it bound to the object the method was called from, its otherwise bound to undefined or the global object (depending on the 'strict mode' pragma), and can of course be set with Function.prototype.bind et al.

But arrow functions have no binding for the this pointer created by the runtime (nor can it be specified via Function.prototype.bind), meaning it gets lexically looked up through scope chain resolution just like any other var. The MDN article is at best slightly confusing on this point (see link above).

Additionally, arrow functions have an implicit return, the return value will automatically be the last evaluated expression in the function body.

Arrow functions have no arguments psuedo-array. You can use ES 6 rest parameters instead.

For functions of arity 1, the parens around the parameter may be omitted.

like image 196
Jared Smith Avatar answered Sep 23 '22 20:09

Jared Smith


That's an es6 arrow function. If you switch to WebStorms settings:

enter image description here

you can switch your javascript version to ecmascript 6 like shown in the picture to make WebStorm recoginze them properly.


like image 34
baao Avatar answered Sep 24 '22 20:09

baao