7 JavaScript Concepts to Master Before You Learn React
So you are an aspiring web developer and have a good understanding of web technologies like HTML, CSS, and JavaScript. Now, assuming that you have watched tutorials, read blog posts, documentations and listened to podcasts on Web development, you must have frequently heard that React is the most popular JavaScript library out there in the field of web development.
Now, given that you have built some sites using HTML, CSS, and JavaScript, you might wonder if you are ready to dive into the world of React Library after all it is based on JavaScript. This article will describe a set of JavaScript concepts that one needs to master before one gets their feet wet at learning React.
- Fundamental of JavaScript
- Arrow Function
- Array methods
- Async and await
- Template literal
- Spread operator
- Destructuring
1. Fundamental of JavaScript
The fundamentals of JavaScript include variable declaration, data types, operators, control statements, functions, objects, comments, events, etc.
2. Arrow Function
The arrow function is a popular alternative to the traditional approach to writing Javascript functions in a more compact manner. The arrow function reduces the lines of code, hence, enhances readability.
It offers more flexibility and automatically binds the ‘this’ keyword.
const sum = (a, b)=> a + b;
console.log(sum(4, 6));
Output: 5
3. Array methods
Array methods are one of the most commonly used JavaScript concepts in React since arrays are needed every time a set of data needs to be handled. The important array methods to learn are as follows:
- map(): It allows you to iteratively access each item of an array and return a new array using the items of the original array.
- forEach(): It is similar to the map method that iteratively accesses each item of an array. But unlike a map method, it does not return a new array but instead modifies the original array.
- filter(): It iteratively accesses each item of an array and returns a new array filtering out the data item of the array that does not satisfy the given condition.
- reduce(): Reducer executes the reducer function for every array element and returns a single value. The original array remains unchanged.
- find(): It returns the first item in the array that satisfies the condition.
- includes(): It returns true if the array contains the item passed as a parameter.
4. Async and await
JavaScript is a single-threaded language and is synchronous in nature by default. So when executing certain functions that take some time to execute, for example, making API calls to fetch data, the other functionalities freeze for the time being. So to overcome this limitation, we use async and await to make the code asynchronous.
async function getItemsInfo(name) {
const object await server.getObject();
const object = object.find(object => { return object.name return object; name });
}
5. Template Literal
Every once in a while, you run into a situation where you have to interpolate variables and expressions into strings. The best way to do this is by using template literal, which is a new feature of ECMAScript 6. It has backticks instead of inverted commas.
const username = "John";
console.log(`Hello, ${username}`);
Output: Hello, John
6. Spread Operator
The primary purpose of the Spread operator is to allow copying all or part of an array or object into another array or object. It is a feature of EcmaScript 6 of Javascript.
const array1 = ["apple", "samsung", "redmi"];
const array2 = [...array1];
console.log(array2);
Output: [ ‘apple’, ‘samsung’, ‘redmi’ ]
7. Destructuring
The purpose of destructuring in JavaScript is to extract the data from arrays and objects into variables already defined in the source array or object. A user-defined variable can also be used to contain the value obtained from extraction as shown in the figure below.
const person ={
name: "John Snow",
location: "England"
}
const { name, location: country } = person;
console.log(name + " from " + country);
Output: John Snow from England
Besides these concepts, you should learn the ternary operator, this keyword, object, and nested objects, let, const and var keywords and their differences before learning React.