React Js
#5 ES6 Basic - Arrow Function in Javascript - React Basic to Pro
What is function
Functions () are Statements that Do some things.
What is ES6 Arrow function
Arrow functions are ES6 short hand function syntax, lighter and smarter.
Traditional function example
This function will get score with 20 added.
const getScore = function (score) {
return score + 20;
};
Now write the same code in ES6 arrow function -
const getScore = (score) => score + 20;
So, It's really simple and shorter. Which things are removed then in arrow function are below -
- Arrow function returns value by default when in one line resulting in shorter and simpler code in some cases
- If More than one line, then it needs {} curly braces.
- Arrow function is supported for multiple parameter also as expected.
Here is a detail example.
const getScore2 = () => 100;
const totalScore = (score1, score2) => {
if (score1 > 50) {
return score2;
} else {
return score1 + score2;
}
};
console.log(getScore(10));
console.log(getScore2());
console.log(totalScore(10, 1000));
Simple API Call Example with ES5 function and ES6 Arrow function -
// Traditional function
function getRepositories() {
return fetch("https://api.github.com/users/maniruzzamanakash/repos")
.then(function (response) {
return response.json();
})
.catch(function (error) {
return error;
});
}
// Arrow function
const getRepositoriesArrowFunc = () => {
return fetch("https://api.github.com/users/maniruzzamanakash/repos")
.then((response) => response.json())
.catch((error) => error);
};
console.log(getRepositories());
console.log(getRepositoriesArrowFunc());
So, we've see the almost same work's function is better at arrow function.
Filter Implementation in ES6 arrow function Example -
const arrays = [10, 20, 300, 400];
let newArray = [];
arrays.forEach(function (item) {
if (item > 100) {
newArray.push(item);
}
});
console.log(newArray);
const newArrayArrowFunc = arrays.filter((item) => item > 100);
console.log(newArrayArrowFunc);
So, look at this function. In arrow function it's just one line of code and really it's very simpler, readable and optimized.
Problems:
Array function is not good with this
keywords as before. See the code example -
const address = {
city: "Dhaka",
zip: 1213,
details: function () {
return `You Live in ${this.city} at zip - ${this.zip}`; // OK
},
};
const addressArrowFunc = {
city: "Dhaka",
zip: 1213,
details: function () {
return `You Live in ${this.city} at zip - ${this.zip}`; // Error
},
};
console.log(address.details());
console.log(addressArrowFunc .details());
So, definitely we should use arrow function in almost all cases of javascript
#4 - let vs const vs var - Clear the ES6 concepts and makes simpler path to learn React
Higher Order Component in React - In depth discussion about React HOC
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development