Function vs Arrow Function in JavaScript: 3 Differences You Must Know #shorts
Description: Regular function or arrow function? They look almost the same, but they behave differently.
In this short:
1. Syntax: arrow functions are shorter and return one expression automatically
2. Hoisting: you can call a regular function before it's defined, but an arrow function in a const throws a ReferenceError
3. this: arrow functions don't have their own this. They use the this from the surrounding code
Bonus: arrow functions can't be used with new. They're not constructors
Use arrow functions for short callbacks like map, filter and setTimeout. Use regular functions for object methods and when you need to call a function before it's defined.
Subscribe to BentHub for more JavaScript tips!
#javascript #webdevelopment #coding #programming #js #frontend #learntocode Close description
In this short:
1. Syntax: arrow functions are shorter and return one expression automatically
2. Hoisting: you can call a regular function before it's defined, but an arrow function in a const throws a ReferenceError
3. this: arrow functions don't have their own this. They use the this from the surrounding code
Bonus: arrow functions can't be used with new. They're not constructors
Use arrow functions for short callbacks like map, filter and setTimeout. Use regular functions for object methods and when you need to call a function before it's defined.
Subscribe to BentHub for more JavaScript tips!
#javascript #webdevelopment #coding #programming #js #frontend #learntocode Close description




