Search code examples
javascriptecmascript-6ecmascript-5arrow-functions

Converting an es6 arrow function to es5 manually?


How do we convert the arrow function below to es5 manually?

e => varName = e.target.value

My attempt:

function(e) {
 varName = e.target.value
 return varName
}

Am I right?

What about this below?

varName = function(e) {
 return e.target.value
}

Solution

  • Both of them are extremely similar and perform nearly the same task, however the ES6 function implicitly creates a global variable named varName and assigns it a value, and neither of the ES5 functions do that. This one is pretty much exactly the same:

    function(e) {
      return varName = e.target.value;
    }
    

    Or:

    function(e) {
      varName = e.target.value;
      return varName;
    }