Arrow Function in JavaScript

Arrow functions also called “fat arrow” functions are a more concise syntax for writing function expressions.
Arrow functions were introduced with ES6 as a new syntax for writing JavaScript functions.
fat arrow symbol ( => ).

Without Arrow Function:
testFunc = function() {
  return “Welcome to InfallibleTechie”;
}

With Arrow Function:
testFunc = () => {
  return “Welcome to InfallibleTechie”;
}

With Arrow Function with Parameters:
<p id=”test”></p>
<script>
var hello;
hello = (val1, val2) => val1 + val2;
document.getElementById( “test” ).innerHTML = hello( 3, 2 );
</script>

Output is 5.

this keyword impact:
Arrow functions allow you to retain the scope of the caller inside the function.
The value of “this” inside a function can’t be changed. It will be the same value as when the function was called.
If you need to bind to a different value, you’ll need to use a function expression.

Leave a Reply