Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does => mean in JavaScript? (equals greater than) [duplicate]

tl;dr: what does => do?

I've just finished solving a problem on codewars and after looking at other peoples' common responses to the problem, I keep seeing this: =>

The problem is below:

You have a quiver of arrows, but some have been damaged. The quiver contains arrows with an optional range information (different types of targets are positioned at different ranges), so each item is an arrow. You need to verify that you have some good ones left, in order to prepare for battle. The below is an example array which is the quiver of arrows.

anyArrows([
{range: 5}, 
{range: 10, damaged: true}, 
{damaged: true}
])

If an arrow in the quiver does not have a damaged status, it means it's new.

This is an example I saw which returns true or false, depending on if there is an undamaged arrow in the quiver:

function anyArrows(arrows){
  return arrows.some(a => !a.damaged);
}

Now, that was way shorter than my code! Mine was a lot more basic:

function anyArrows(arrows){
  for ( var i = 0 ; i < arrows.length ; i++ ){
    if ( arrows[i].damaged === false ) { return true; }
    else if (arrows[i].damaged === true) { return false; }
    else if (arrows[i].range === 0) { return false }
    else { return true; } 
  }
  if (arrows.length === 0) return false;
}

Again though, the question is: what does => do in this case and in general?

like image 743
christopherchapman Avatar asked Sep 30 '15 12:09

christopherchapman


People also ask

What does => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What does >= do in JavaScript?

The greater than or equal operator ( >= ) returns true if the left operand is greater than or equal to the right operand, and false otherwise.

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

The difference between == and === is that: == converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

What does === comparison do?

The === operator compares operands and returns true if both operands are of the same data type and have some value, otherwise, it returns false. The !== operator compares operands and returns true if both operands are of different data types or are of the same data type but have different values.


1 Answers

=> is ES2015 syntax that separates arrow function parameters from body, e.g. (params) => { /* body */ }.

ArrowFunction : ArrowParameters => ConciseBody

like image 196
JMM Avatar answered Oct 04 '22 10:10

JMM