
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Difference between regular functions and arrow functions in JavaScript
According to MDN, An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
There are 3 subtle differences in regular functions and arrow functions in JavaScript.
No own this bindings
Arrow functions do not have their own this value. The value of this inside an arrow function is always inherited from the enclosing scope.
Example
this.a = 100; let arrowFunc = () => {this.a = 150}; function regFunc() { this.a = 200; } console.log(this.a) arrowFunc() console.log(this.a) regFunc() console.log(this.a)
Output
100 150 150
See that the arrow function changed the this object outside its scope. The regular function just made the changes within its own this.
Arrow functions do not have a arguments array
In JS arguments array in functions is a special object that can be used to get all the arguments passed to the function. Similar to this, arrow functions do not have their own binding to a arguments object, they are bound to arguments of enclosing scope.
Arrow functions are callable but not constructable
If a function is constructable, it can be called with new, i.e. new User(). If a function is callable, it can be called without new (i.e. normal function call).
Functions created through function declarations / expressions are both constructable and callable.
Arrow functions (and methods) are only callable. class constructors are only constructable.
If you are trying to call a non-callable function or to construct a non-constructable function, you will get a runtime error.
let arrowFunc = () => {} new arrowFunc() This code gives the error: arrowFunc is not a constructor
- Related Articles
- Regular functions vs Arrow functions in JavaScript?
- Arrow functions in Javascript
- Lambdas with Arrow Functions in JavaScriptLambdas with Arrow Functions in JavaScript
- Fat arrow functions in JavaScript
- Concise arrow functions in JavaScript
- Higher-Order Arrow Functions in JavaScript.
- Fat vs concise arrow functions in JavaScript
- When you should not use JavaScript Arrow Functions?
- What is the difference between functions and methods in JavaScript?
- What is difference between unescape() and escape() functions in JavaScript?
- How to use arrow functions used as methods in JavaScript?
- What is the difference between anonymous and inline functions in JavaScript?
- What is the difference between closure and nested functions in JavaScript?
- What is the difference between custom and built-in functions in JavaScript?
- What is the difference between default and rest parameters in JavaScript functions?
