DEV Community

Cover image for Core JavaScript Concepts to Master Before Learning React
Tunde Fadipe
Tunde Fadipe

Posted on

Core JavaScript Concepts to Master Before Learning React

It's widely recommended to have a solid understanding of JavaScript, or at least some key JavaScript concepts, before delving into learning React.js. React is a JavaScript library primarily used for creating user interfaces (UIs) in web applications. Its reputation for speed, scalability, and simplicity makes it a popular choice among developers. In this article, we'll explore the fundamental JavaScript concepts that are crucial prerequisites for mastering React. Here are the key JavaScript concepts you should familiarize yourself with:

A. Variables and Data Types:

  • Understand how to declare variables using var, let, and const.
  • Be familiar with primitive data types (e.g., numbers, strings, booleans) and complex data types (e.g., objects, arrays). Example:
// Variables and Data Types in JavaScript

// Declaring variables using 'var' (older way), 'let' (modern way), and 'const' (constant)
var name = "John"; // String data type
let age = 30; // Number data type
let isStudent = false; // Boolean data type
const PI = 3.14159265359; // Constant number (cannot be reassigned)

// Printing variables to the console
console.log("Name:", name); // Output: Name: John
console.log("Age:", age); // Output: Age: 30
console.log("Is Student:", isStudent); // Output: Is Student: false
console.log("PI Value:", PI); // Output: PI Value: 3.14159265359

// Reassigning a variable (let can be reassigned, const cannot)
name = "Alice"; // Variable 'name' is reassigned
console.log("Updated Name:", name); // Output: Updated Name: Alice

// Trying to reassign a constant variable will result in an error
// PI = 3.14; // Uncommenting this line will produce an error
// console.log("Updated PI Value:", PI); // This line will not be executed

// JavaScript supports dynamic typing, so you can change the data type of a variable
age = "Twenty"; // Now 'age' holds a string value
console.log("Updated Age:", age); // Output: Updated Age: Twenty

// JavaScript has other data types too, like null and undefined
let address = null; // Null represents the absence of a value
let phoneNumber; // Undefined is used when a variable is declared but not assigned a value

console.log("Address:", address); // Output: Address: null
console.log("Phone Number:", phoneNumber); // Output: Phone Number: undefined
Enter fullscreen mode Exit fullscreen mode

In this code:

  • We declare variables name, age, and isStudent using both var and let to demonstrate the difference between them.

  • Each variable holds a different data type: name is a string, age is a number, and isStudent is a boolean.

  • We print the variables to the console using console.log().

  • We reassign the name variable to show how you can change the value of a variable.

  • We illustrate that JavaScript supports dynamic typing, allowing you to change the data type of a variable.

  • We introduce the null and undefined data types and demonstrate their usage.
    This code provides a simple overview of variables and data types in JavaScript, showcasing the flexibility of the language in handling different types of data.

B. Functions:

  • Learn how to define and call functions.
  • Understand function parameters and return values.
  • Explore concepts like function expressions and arrow functions introduced in ES6. Example:
// Functions in JavaScript

// Defining a function
function greet(name) {
  console.log("Hello, " + name + "!");
}

// Calling a function
greet("John"); // Output: Hello, John!
greet("Alice"); // Output: Hello, Alice!

// Function with parameters and return value
function add(a, b) {
  return a + b;
}

// Calling a function with parameters and storing the result
let sum = add(5, 3);
console.log("Sum:", sum); // Output: Sum: 8

// Function expression (anonymous function)
const multiply = function(x, y) {
  return x * y;
};

let product = multiply(4, 2);
console.log("Product:", product); // Output: Product: 8

// Arrow function (ES6+)
const divide = (numerator, denominator) => numerator / denominator;

let quotient = divide(10, 2);
console.log("Quotient:", quotient); // Output: Quotient: 5

// Arrow function with no parameters
const sayHello = () => {
  console.log("Hello from the arrow function!");
};

sayHello(); // Output: Hello from the arrow function!

Enter fullscreen mode Exit fullscreen mode

In this code:

  • We define a function greet that takes one parameter (name) and logs a greeting message to the console.

  • We call the greet function twice with different arguments to demonstrate how functions can be reused with different inputs.

  • Next, we define a function add that takes two parameters (a and b) and returns their sum using the return statement.

  • We call the add function with arguments 5 and 3, store the result in the sum variable, and then log the result to the console.

  • We introduce a function expression by defining the multiply function using the const keyword. Function expressions allow you to create anonymous functions that can be assigned to variables.

  • We call the multiply function and store the result in the product variable.

  • Finally, we demonstrate the use of arrow functions (introduced in ES6). The divide function is defined using the arrow function syntax and then called with two arguments. The result is stored in the quotient variable.

This code provides a clear explanation of how to define, call, and work with functions in JavaScript, including regular functions, function expressions, and arrow functions.

C. Control Structure:
Control structures in JavaScript allow you to control the flow of your program by making decisions and repeating actions. They include conditional statements and loops. Here, I'll explain control structures with code examples for each type.
a. Conditional Statements:
Conditional statements allow you to execute different blocks of code based on specified conditions. The most common types are if, else if, and else.
Example:

let age = 20;

if (age >= 18) {
  console.log("You are an adult.");
} else if (age >= 13) {
  console.log("You are a teenager.");
} else {
  console.log("You are a child.");
}

Enter fullscreen mode Exit fullscreen mode

In this example:

  • We use the if statement to check if age is greater than or equal to 18.

  • If the condition is true, it prints "You are an adult."

  • If the condition is false, it checks the next condition using else if.

  • If none of the conditions are true, it falls back to the else block.

b. Loops:
Loops allow you to repeatedly execute a block of code as long as a specified condition is true. Common loop types are for, while, and do...while.
Example:

// Using a for loop to print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
  console.log(i);
}

// Using a while loop to print numbers from 1 to 5
let counter = 1;
while (counter <= 5) {
  console.log(counter);
  counter++;
}

// Using a do...while loop to print numbers from 1 to 5
let num = 1;
do {
  console.log(num);
  num++;
} while (num <= 5);

Enter fullscreen mode Exit fullscreen mode

In these examples:

  • The for loop iterates from 1 to 5 using a counter variable i.

  • The while loop and do...while loop do the same but with different syntax.

c. Switch Statement:
A switch statement allows you to perform different actions based on different conditions.
Example:

let day = "Monday";

switch (day) {
  case "Monday":
    console.log("It's the start of the workweek.");
    break;
  case "Friday":
    console.log("It's almost the weekend!");
    break;
  default:
    console.log("It's some other day.");
}

Enter fullscreen mode Exit fullscreen mode

Here:

  • We evaluate the value of day against different cases.
  • When a case matches, the code inside that case block is executed.
  • The break statement is used to exit the switch statement after a case is matched. These control structures are fundamental for making decisions and repeating tasks in JavaScript, allowing you to create dynamic and responsive programs.

D. Objects and Object-Oriented Programming (OOP):
Objects and Object-Oriented Programming (OOP) are fundamental concepts in JavaScript. JavaScript is an object-oriented language, and objects are central to how it models data and behavior. Here, I'll explain objects and OOP in JavaScript with a code example.
Objects in JavaScript:
In JavaScript, an object is a collection of key-value pairs, where each key is a string (also called a property name), and each value can be of any data type, including other objects or functions. Objects allow you to structure and organize data and behavior in a logical way.

// Creating an object
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  sayHello: function() {
    console.log(`Hello, my name is ${this.firstName} ${this.lastName}.`);
  }
};

// Accessing object properties
console.log(person.firstName); // Output: John
console.log(person.age); // Output: 30

// Calling an object method
person.sayHello(); // Output: Hello, my name is John Doe.

Enter fullscreen mode Exit fullscreen mode

In this example:

  • We create an object named person with properties like firstName, lastName, and age. Properties can store data of various types.

  • We define a method sayHello within the object, which is a function attached to the object and can access its properties using the this keyword.
    Object-Oriented Programming (OOP) in JavaScript:
    JavaScript supports object-oriented programming, which allows you to create classes and objects to model real-world entities and their interactions. ES6 introduced class syntax for defining classes and constructor functions.

// Creating a class using ES6 class syntax
class Person {
  constructor(firstName, lastName, age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
  }

  sayHello() {
    console.log(`Hello, my name is ${this.firstName} ${this.lastName}.`);
  }
}

// Creating objects (instances) from the class
const person1 = new Person("John", "Doe", 30);
const person2 = new Person("Alice", "Smith", 25);

// Accessing object properties and methods
console.log(person1.firstName); // Output: John
console.log(person2.age); // Output: 25
person1.sayHello(); // Output: Hello, my name is John Doe.
person2.sayHello(); // Output: Hello, my name is Alice Smith.

Enter fullscreen mode Exit fullscreen mode

In this example:

  • We define a Person class with a constructor that initializes properties (firstName, lastName, and age) when objects are created from the class.

  • The sayHello method is defined within the class to provide behavior.

  • We create two instances of the Person class, person1 and person2, and access their properties and methods.
    JavaScript's object-oriented features allow you to encapsulate data and behavior within objects and classes, making your code more organized, reusable, and maintainable. Objects and OOP principles are widely used in JavaScript to create complex systems and applications.

E. Arrays in JavaScript:

  • Arrays are collections of values, enclosed in square brackets [].

  • They can store various data types like numbers, strings, and objects.

  • You can access elements using indexes (0-based).

const fruits = ["apple", "banana", "cherry"];

console.log(fruits[0]); // Output: "apple"

Enter fullscreen mode Exit fullscreen mode

Iteration (Looping) in JavaScript:

Iteration is a way to go through each element in an array.
You can use for loops, forEach, or for...of loops.
It allows you to perform actions on array elements.

// Using a for loop
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

// Using forEach
fruits.forEach(function(fruit) {
  console.log(fruit);
}

// Using for...of (ES6+)
for (const fruit of fruits) {
  console.log(fruit);
}

Enter fullscreen mode Exit fullscreen mode

Iterating through arrays is vital for tasks like calculations, filtering, or displaying data, making your code more efficient and dynamic.
F. Asynchronous JavaScript:
Asynchronous JavaScript allows you to perform tasks concurrently without blocking other code execution. Key concepts include:

  • Callbacks: Functions that run after asynchronous operations complete. Example:
fetchData(function(data) {
  console.log(data);
});

Enter fullscreen mode Exit fullscreen mode
  • Promises: A cleaner way to handle async operations, introduced in ES6. Example:
fetch(url)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

Enter fullscreen mode Exit fullscreen mode
  • Async/Await: Syntactic sugar for working with Promises, improving code readability. Example:
async function fetchData() {
  try {
    const response = await fetch(url);
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}

Enter fullscreen mode Exit fullscreen mode

These asynchronous techniques are crucial for handling tasks like network requests, file I/O, and user interactions without freezing the application.

G. JavaScript ES6+ Features:
ES6+ (ECMAScript 2015 and beyond) introduced several new features and enhancements to JavaScript, making the language more powerful and expressive. These features includes:

  • Arrow Functions: Shorter syntax for defining functions.
// ES5
function add(a, b) {
  return a + b;
}

// ES6+
const add = (a, b) => a + b;

Enter fullscreen mode Exit fullscreen mode
  • Let and Const: Block-scoped variable declarations with let and constants with const.
let variable = 42;
const constantValue = "hello";

Enter fullscreen mode Exit fullscreen mode
  • Template Literals: Improved string interpolation.
const name = "Alice";
console.log(`Hello, ${name}!`);

Enter fullscreen mode Exit fullscreen mode
  • Destructuring: Extracting values from objects and arrays.
const { firstName, lastName } = person;
const [first, second] = numbers;

Enter fullscreen mode Exit fullscreen mode
  • Spread/Rest Operator: Spreading elements into arrays or objects and gathering them.
const mergedArray = [...arr1, ...arr2];
const { prop1, ...rest } = obj;

Enter fullscreen mode Exit fullscreen mode
  • Classes: A more structured way to create objects using class syntax.
class Person {
  constructor(name) {
    this.name = name;
  }
}

Enter fullscreen mode Exit fullscreen mode
  • Promises: Improved async programming with Promises for handling asynchronous operations.
fetch(url)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

Enter fullscreen mode Exit fullscreen mode
  • Async/Await: Syntactic sugar for handling Promises more elegantly.
async function fetchData() {
  try {
    const response = await fetch(url);
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error(error);
  }

Enter fullscreen mode Exit fullscreen mode

H. Modules in JavaScript:
Modules and modular development in JavaScript refer to the practice of organizing code into separate, reusable, and independent modules or files. This approach improves code organization, maintainability, and collaboration.
Example:
Suppose you are building a web application. Instead of putting all your code in a single, massive JavaScript file, you can create separate modules for different functionalities:

// Module 1: user.js
export function getUser(id) {
  // Code to fetch user data
}

// Module 2: post.js
export function createPost(data) {
  // Code to create a new post
}

// Module 3: main.js
import { getUser } from './user.js';
import { createPost } from './post.js';

// Use functions from user.js and post.js to build your application

Enter fullscreen mode Exit fullscreen mode

In this example:

user.js and post.js are separate modules, each handling specific tasks related to users and posts.
The import and export statements allow you to use functions from one module in another.
By breaking down your application into modules, you create a more organized and maintainable codebase.

I. AJAX and API Interaction in JavaScript:
AJAX (Asynchronous JavaScript and XML):

  • AJAX is a technique in web development that allows you to make asynchronous requests to a server without reloading the entire web page. -AJAX typically uses the XMLHttpRequest object or the fetch API to make asynchronous requests to a server.
  • The response from the server can be in various formats, such as JSON, XML, or HTML.
    API Interaction (Application Programming Interface):

  • An API is a set of rules and protocols that allows different software applications to communicate and interact with each other.

  • In web development, APIs are often used to request and exchange data between a web application and a remote server.

In summary, AJAX enables the asynchronous retrieval of data from a server, while API interaction defines the structured way in which web applications communicate with external services or data sources, enhancing the functionality and interactivity of web applications.
Example:
Here's a concise code example that demonstrates AJAX and API interaction in JavaScript using the fetch API to retrieve data from a fictitious API:

// AJAX and API Interaction Example

// Make an API request using the fetch API
fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return response.json(); // Parse the response as JSON
  })
  .then(data => {
    // Use the retrieved data
    console.log('API Response:', data);
  })
  .catch(error => {
    // Handle errors
    console.error('Error:', error);
  });

Enter fullscreen mode Exit fullscreen mode

Conclusion
In this article, we've explored more than 8 fundamental JavaScript methods and concepts that are essential prerequisites for a solid foundation before diving into React. While there are numerous other valuable JavaScript topics to explore, these particular concepts may sometimes be overlooked during initial JavaScript learning. It's crucial to grasp these fundamentals before embarking on your React journey.

If you're just starting with JavaScript, I would like to recommend using javascript.info resources as part of your learning materials to aid in your understanding of JavaScript concepts and topics.

This is my first ever article. Your critics and comments will be highly valued. Thank you

Top comments (0)