re: Why I use JS. VIEW POST


The x=>x/2 function is a lambda expression, a.k.a. arrow function in case you are wondering. The same feature is also seen in other languages such as Java since Java 8 and C#.


Yeah, I know about that, and it is a thing that boosts how fast you can write code IMO. If that's wrong in your opinion, tell me!


In JavaScript's case, it helps with some this issues.

// Shamelessly copied code from MDN added with logging
function Person(){
  this.age = 0;

  setInterval(() => {
    console.log(this.age++); // |this| properly refers to the Person object
  }, 1000);

var p = new Person();

The callback here in setInterval works fine and well (logging 0,1,2 etc.), yet without using arrow functions (i.e. before ES6), the callback would look like:

setInterval(function() {
  console.log(this.age++); // JS be like, "What's this?"
}, 1000);

The this in the callback function refers to Window (logging NaN) and all hell breaks loose.

Your point of writing faster is true, but it has other benefits depending on the language too 😉 (in Java's case, a more compact code and easier code tracing, plus introducing functional programming to its OOP world).

code of conduct - report abuse