- JS Introduction
- JS Introduction
- JS Comments
- JS Variables
- JS Datatypes
- JS Operators
- JS Type Conversions
- JS Control Flow
- JS Comparisons
- JS If else
- JS If else Ladder
- JS Ternary Operator
- JS Switch
- JS For Loop
- JS For In
- JS For Of
- JS While
- JS Do While
- JS Break & Continue
- JS Functions
- JS Function Declaration
- JS Function Parameters
- JS Return Statement
- JS Function Expressions
- JS Anonymous Functions
- JS Objects
- JS Objects
- JS Object Methods
- JS Object Constructors
- JS Object Destructuring
- JS Object Prototypes
- JS Map, Filter & Reduce
- JS ES6
- JS ES6
- JS let and const
- JS Arrow Functions
- JS Template Literals
- Destructuring Assignment
- JS Spread Operator
- JS Default Parameters
- JS Classes
- JS Inheritance
- JS Map
- JS Set
- JS Async
- JS Callbacks
- JS Asynchronous
- JS Promises
- JS Async/Await
- JS HTML DOM/BOM
- JS Document Object
- JS getElementbyId
- getElementsByClassName
- JS getElementsByName
- getElementsByTagName
- JS innerHTML
- JS outerHTML
- JS Window Object
- JS History Object
- JS Navigator Object
- JS Screen Object
JavaScript Arrow Function
Introduced in ES6 (ECMAScript 2015), arrow functions have become the standard for writing modern JavaScript. They offer a more compact syntax and handle scope in a way that solves many common "bugs" associated with traditional function expressions.
Concise Syntax:
- Arrow functions provide a more concise syntax for defining functions by removing the need for the
functionkeyword and, in many cases, thereturnkeyword and curly braces. - Especially useful for short, one-line functions and callbacks where brevity improves code readability.
Lexical this:
- Unlike regular functions, arrow functions do not have their own
this. Instead, they inherit this from the enclosing lexical scope. This is incredibly useful when working with timers or event listeners where you want to maintain access to a class or object context.
this, you should avoid using them as methods inside objects if you need to access other properties of that object using this.propertyName.
No arguments Object:
- Arrow functions do not have their own arguments object. If you try to access
argumentsinside an arrow function, it will refer to the arguments of the nearest non-arrow parent function.
(...args) => { ... }) instead of the old arguments object.
No Binding of this, super, new.target, and arguments:
- Arrow functions do not bind their own this, super, new.target, or arguments. This makes them "lightweight" and predictable, but it also means they cannot be used as constructors.
new keyword with an arrow function. Arrow functions lack a [[Construct]] method and will throw a TypeError if you try to instantiate them.
Example: Basic Arrow Function
In this basic version, we use parentheses for parameters and an arrow to point to the function body.
// Traditional Function
const add = function(a, b) {
return a + b;
};
// Arrow Function
const add = (a, b) => a + b;
Example: Arrow Function with Implicit Return
If the function body consists of only one expression, you can omit the curly braces and the return keyword. The result of the expression is returned automatically.
const square = x => x * x;
console.log(square(5)); // Output: 25
Example: Arrow Function with No Parameters
When there are no input parameters, you must provide an empty set of parentheses to keep the syntax valid.
const greet = () => "Hello, World!";
Example: Arrow Function in Array map
This is a real-world scenario where arrow functions shine, allowing you to transform data with minimal boilerplate.
const numbers = [1, 2, 3];
// Using an arrow function to double each number in the array
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
Key Points
- Shorter Syntax: Arrow functions reduce "syntactic noise," allowing the logic of your code to stand out.
- Lexical Scoping: They inherit
thisfrom the surrounding code, making them perfect for callbacks inside classes or closures. - Ideal for Anonymous Functions: They are the go-to choice for short, inline logic, especially in modern frameworks like React, Vue, or Angular.
- Limitations: Remember that they cannot be used as generators, and they cannot be used as constructors for objects.