JavaScript Concepts to Know Before Learning React

If you want to learn React –or any JavaScript framework – you'll first need to understand the fundamental JavaScript methods and concepts.
Many developers choose a "learn as you go" approach when learning React. But this often doesn't result in productivity, and instead worsens the gaps in their JavaScript knowledge. This approach makes assimilating each new feature twice as difficult (you might begin to confuse JavaScript with React).
React is a JavaScript framework for building UI components-based user interfaces. All of its code is written in JavaScript, including the HTML markup, which is written in JSX (this enables developers to easily write HTML and JavaScript together).

Lab Learning Outcomes:

Go over all of the JS ideas and techniques you'll need to grasp before learning React.
React is built using modern JavaScript features, which were mostly introduced with ES2015.

The JavaScript You Need to Know Before Learning React

Callback Functions in JavaScript
A callback function is a function that is performed after another function has completed its execution. It is typically supplied as an input into another function.
Callbacks are critical to understand since they are used in array methods (such as map(), filter(), and so on), setTimeout(), event listeners (such as click, scroll, and so on), and many other places.
Here's an example of a "click" event listener with a callback function that will be run whenever the button is clicked:
<button class="btn">Click Me</button>
const btn = document.querySelector('.btn');
btn.addEventListener('click', () => {
let name = 'John doe';
NB: A callback function can be either an ordinary function or an arrow function.
Promises in JavaScript
As previously stated, a callback function is executed after the original function is executed. You may now begin to consider stacking so many callback functions on top of each other because you do not want a specific function to run until the parent function has finished running or a specific time has passed.

For example, let's attempt to display 5 names in the console after 2 seconds each – that is, the first name appears after 2 seconds, the second after 4 seconds, and so on...
setTimeout(() => {
setTimeout(() => {
setTimeout(() => {
setTimeout(() => {
setTimeout(() => {
}, 2000);
}, 2000);
}, 2000);
}, 2000);
}, 2000);
This above example will work, but it will be difficult to comprehend, debug, or even add error handling to. This is referred to as"Callback Hell". Callback hell is a big issue caused by coding with complex nested callbacks.
The primary reason for using promises is to prevent callback hell. With Promises, we may write asynchronous code in a synchronous manner.
A promise is an object that returns a value that you anticipate to see in the future but do not now see.
A practical use for promises would be in HTTP requests, where you submit a request and do not receive a response right away because it's an asynchronous activity. You only receive the answer (data or error) when the server responds.

JavaScript promise syntax:

const myPromise = new Promise((resolve, reject) => {
// condition
Promises have two parameters, one for success (resolve) and one for failure (reject). Each has a condition that must be satisfied in order for the Promise to be resolved – otherwise, it will be rejected:
const promise = new Promise((resolve, reject) => {
let condition;
if(condition is met) {
resolve('Promise is resolved successfully.');
} else {
reject('Promise is rejected');
There are 3 states of the Promise object:
Pending: by default, this is the Initial State, before the Promise succeeds or fails.
Resolved: Completed Promise
Rejected: Failed Promise
Finally, let's try to re-implement the callback hell as a promise:
function addName (time, name){
return new Promise ((resolve, reject) => {
reject('No such name');
addName(2000, 'Joel')
.then(()=>addName(2000, 'Victoria'))
.then(()=>addName(2000, 'John'))
.then(()=>addName(2000, 'Doe'))
.then(()=>addName(2000, 'Sarah'))

Map() in JavaScript

One of the most often used methods is, which allows you to iterate over an array and modify its elements using a callback function. The callback function will be run on each array element.
Assume we have an array of users that contains their information.
let users = [
{ firstName: "Susan", lastName: "Steward", age: 14, hobby: "Singing" },
{ firstName: "Daniel", lastName: "Longbottom", age: 16, hobby: "Football" },
{ firstName: "Jacob", lastName: "Black", age: 15, hobby: "Singing" }
We can loop through using map and modify it’s output
let singleUser =>{
//let's add the firstname and lastname together
let fullName = user.firstName + ' ' + user.lastName;
return `
<h3 class='name'>${fullName}</h3>
<p class="age">${user.age}</p>
You should note that:
map() always returns a new array, even if it’s an empty array.
It doesn’t change the size of the original array compared to the filter method
It always makes use of the values from your original array when making a new one.
Gotcha: The map method works almost like every other JavaScript iterator such as forEach() but it’s proper to always use the map method whenever you are going to return a value.
Here is a perfect description by
One of the key reasons we use map is so we can encapsulate our data in some HTML, whereas for React this is simply done using JSX.
You can read more about map() by searching on MSDN.
Filter() and Find() in JavaScript
Filter() provides a new array depending on certain criteria. Unlike map(), it can alter the size of the new array, whereas find() returns just a single instance (this might be an object or item). If several matches exist, it returns the first match – otherwise, it returns undefined.
Suppose you have an array collection of registered users with different ages:
let users = [
{ firstName: "Susan", age: 14 },
{ firstName: "Daniel", age: 16 },
{ firstName: "Bruno", age: 56 },
{ firstName: "Jacob", age: 15 },
{ firstName: "Sam", age: 64 },
{ firstName: "Dave", age: 56 },
{ firstName: "Neils", age: 65 }
You could choose to sort this data by age groups, such as young individuals (ages 1-15), senior people (ages 50-70), and so on...
In this case, the filter function comes in handy as it produces a new array based on the criteria. Let's have a look at how it works.

// for young people
const youngPeople = users.filter((person) => {
return person.age <= 15;
//for senior people
const seniorPeople = users.filter((person) => person.age >= 50);
This generates a new array. It produces an empty array if the condition is not satisfied(no match).
The find() method, like the filter() method, iterates across the array looking for an instance/item that meets the specified condition. Once it finds it, it returns that specific array item and immediately terminates the loop. If no match is discovered, the function returns undefined.
For example:
const Bruno = users.find((person) => person.firstName === "Bruno");
Destructuring Arrays and Objects in JavaScript
Destructuring is a JavaScript feature introduced in ES6 that allows for faster and simpler access to and unpacking of variables from arrays and objects.
Before destructuring was introduced, if we had an array of fruits and wanted to get the first, second, and third fruits separately, we would end up with something like this:
let fruits= ["Mango", "Pineapple" , "Orange", "Lemon", "Apple"];
let fruit1 = fruits[0];
let fruit2 = fruits[1];
let fruit3 = fruits[2];
console.log(fruit1, fruit2, fruit3); //"Mango" "Pineapple" "Orange"
This is like repeating the same thing over and over which could become cumbersome. Let's see how this could be distructured to get the first 3 fruits.
let [fruit1, fruit2, fruit3] = fruits;
console.log(fruit1, fruit2, fruit3); //"Mango" "Pineapple" "Orange"
You might be wondering how you could skip data if you just want to print the first and final fruits, or the second and fourth fruits. You would use commas as follows:
const [fruit1 ,,,, fruit5] = fruits;
const [,fruit2 ,, fruit4,] = fruits;
Object destructuring
Let’s now see how we could destructure an object – because in React you will be doing a lot of object descructuring.
Suppose we have an object of user which contains their firstname, lastname, and lots more,
const Susan = {
firstName: "Susan",
lastName: "Steward",
age: 14,
hobbies: {
hobby1: "singing",
hobby2: "dancing"
In the old way, getting these data could be stressful and full of repetition:
const firstName = Susan.firstName;
const age = Susan.age;
const hobby1 = Susan.hobbies.hobby1;
console.log(firstName, age, hobby1); //"Susan" 14 "singing"
but with destructuring its a lot easier:
const {firstName, age, hobbies:{hobby1}} = Susan;
console.log(firstName, age, hobby1); //"Susan" 14 "singing"
We can also do this within a function:
function individualData({firstName, age, hobbies:{hobby1}}){
console.log(firstName, age, hobby1); //"Susan" 14 "singing"
Rest and Spread Operators in JavaScript
JavaScript spread and rest operators use three dots .... The rest operator gathers or collects items – it puts the “rest” of some specific user-supplied values into a JavaScript array/object.
Suppose you have an array of fruits:
let fruits= ["Mango", "Pineapple" , "Orange", "Lemon", "Apple"];
We could destructure to get the first and second fruits and then place the“rest” of the fruits in an array by making use of the rest operator.
const [firstFruit, secondFruit,] = fruits
console.log(firstFruit, secondFruit, rest); //"Mango" "Pineapple" ["Orange","Lemon","Apple"]
Looking at the result, you'll see the first two items and then the third item is an array consisting of the remaining fruits that we didn't destructure. We can now conduct any type of processing on the newly generated array, such as:
const chosenFruit = rest.find((fruit) => fruit === "Apple");
console.log(`This is an ${chosenFruit}`); //"This is an Apple"
It's important to bear in mind that this has to come last always (placement is very important).
We've just worked with arrays – now let's deal with objects, which are absolutely the same.
Assume we had a user object that has their firstname, lastname, and a lot more. We could destructure it and then extract the remainder of the data.
const Susan = {
firstName: "Susan",
lastName: "Steward",
age: 14,
hobbies: {
hobby1: "singing",
hobby2: "dancing"
const {age,} = Susan;
console.log(age, rest);
This will log the following result:
firstName: "Susan" ,
lastName: "Steward" ,
hobbies: {...}
Let’s now understand how the spread operator works, and finally summarize by differentiating between both operators.
Spread operator
The spread operator, as the name implies, is used to spread out array items. It gives us the ability to get a list of parameters from an array. The spread operator has a similar syntax to the rest operator, except it operates in the opposite direction.
Note: A spread operator is effective only when used within array literals, function calls, or initialized properties objects.
For example, suppose you have arrays of different types of animals:
let pets= ["cat", "dog" , "rabbits"];
let carnivorous = ["lion", "wolf", "leopard", "tiger"];
You might want to combine these two arrays into just one animal array. Let's try it out:
let animals = [pets, carnivorous];
console.log(animals); //[["cat", "dog" , "rabbits"], ["lion", "wolf", "leopard", "tiger"]]
This is not what we want – we want all the items in just one single array. And we can achieve this using the spread operator:
let animals = [...pets, ...carnivorous];
console.log(animals); //["cat", "dog" , "rabbits", "lion", "wolf", "leopard", "tiger"]
This also works with objects. It is important to note that the spread operator cannot expand the values of object literals, since a properties object is not an iterable. But we can use it to clone properties from one object into another.
For example:
let name = {firstName:"John", lastName:"Doe"};
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.