ReactJS - Basics of ES, Babel and npm
Last Updated :
08 Oct, 2024
ReactJS is a popular JavaScript library used to build user interfaces, particularly single-page applications. To develop applications with React, it's essential to understand the tools and technologies that work alongside it. Three fundamental technologies you will encounter in React development are ECMAScript (ES), Babel, and npm.
In this article, we’ll dive into these three core concepts to help you understand their role in ReactJS development.
What is ECMAScript (ES)?
ES is an abbreviation used for ECMAScript. It is a standard for different scripting languages such as JavaScript made by ECMA. ECMA is an abbreviation used for the European Computer Manufacturers Association. It is a standard organization based out of Switzerland which takes care of ES. ES11 (2020) was updated in June 2020 and its nomenclature is like the year in which the update was released then the name will be a year plus one such as ES10 (2019).
In ECMA Script6 also called ES6 and ECMAScript 2015 various new features were added:
The features are listed below:
- The let keyword
- The const keyword
- Arrow functions
- Classes
- Modules
- The Rest Parameters
- The Spread Operator
- Destructuring Assignment
- The Object Destructuring Assignment
- Default values for Function Parameters
- Template Literals
- The for...of Loop
Key Features of ES6+
Some of the key features of ES6+ that are widely used in ReactJS development include:
- Arrow Functions: Provide a shorter syntax for writing functions and automatically bind this.
const greet = (name) => `Hello, ${name}`;
- Classes: Offer a syntactic sugar over the existing prototype-based inheritance model.
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}
- Modules: Allow you to import and export code between files, helping modularize your codebase.
// file1.js
export const greet = () => console.log('Hello');
// file2.js
import { greet } from './file1';
greet(); // Output: Hello
- Destructuring: Allows you to unpack values from arrays or properties from objects.
const person = { name: 'John', age: 30 };
const { name, age } = person;
- Template Literals: Provide an easier way to work with strings.
const name = 'React';
console.log(`Hello, ${name}!`); // Output: Hello, React!
- Promises and Async/Await: Help manage asynchronous operations in a cleaner way.
const name = 'React';
console.log(`Hello, ${name}!`); // Output: Hello, React!
Why ES is Important in ReactJS?
React components and applications are written in modern JavaScript (ES6 and beyond). Features like destructuring, classes, and arrow functions are commonly used to create React components and manage state. A solid understanding of ES6+ syntax will make your ReactJS development experience smoother.
What is Babel?
Babel is a JavaScript compiler that allows developers to use next-generation JavaScript (ES6 and beyond) by converting it into a version of JavaScript that is supported by all browsers (usually ES5). Since not all browsers support modern JavaScript features out of the box, Babel is essential in React development to ensure compatibility across different environments.
How Babel Works?
Babel takes your modern JavaScript code and transforms it into compatible ES5 code. For example, an arrow function written in ES6:
const greet = (name) => `Hello, ${name}`;
Gets compiled by Babel into ES5 syntax:
var greet = function(name) {
return 'Hello, ' + name;
};
Babel also supports the JSX syntax used in React, converting JSX into regular JavaScript function calls.
Setting Up Babel
To set up Babel in your React project, follow these steps:
1. Install Babel: First, you need to install Babel and some necessary presets.
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
2. Create a Babel Configuration File: Add a .babelrc file in your project root and configure it like this:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
3. Run Babel: You can now compile your code using Babel by running:
npx babel src --out-dir dist
This will transpile all the JavaScript and JSX code from the src directory into browser-compatible JavaScript in the dist directory.
What is npm?
npm (Node Package Manager) is the default package manager for Node.js, and it plays an important role in ReactJS development. It is used to manage dependencies (libraries, frameworks, and tools) that you want to include in your project. React itself is distributed as an npm package, and npm makes it easy to manage project dependencies and scripts.
How npm Works?
npm allows you to install, update, and manage JavaScript libraries that your project depends on. These dependencies are listed in a package.json file, which helps keep track of all packages required by your project.
Example of a package.json file:
{
"name": "react-app",
"version": "1.0.0",
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"scripts": {
"start": "webpack serve",
"build": "webpack"
}
}
npm Commands for React Development
Here are some useful npm commands for React development:
1. Initialize a Project: When starting a new project, you can initialize npm by creating a package.json file.
npm init
2. Install a Package: To install a package like React, use:
npm install react
3. Install Development Dependencies: For development tools like Babel or Webpack, you can add --save-dev to install them as development dependencies.
npm install --save-dev webpack babel-loader
4. Run a Script: You can define custom scripts in package.json and run them using:
npm run script-name
In React development, ES6+, Babel, and npm work together to provide a seamless experience:
- ECMAScript: You write your React components using modern JavaScript features (ES6+).
- Babel: Babel compiles your ES6+ code and JSX into browser-compatible JavaScript. It ensures that even browsers that do not support ES6 can run your React application.
- npm: npm handles installing React, Babel, and other dependencies you need for your project. It also helps you run and manage your development environment using scripts and package management.
Here’s an example workflow:
- You write a React component using modern JavaScript (ES6) and JSX.
- Babel compiles this code into browser-compatible ES5.
- npm manages the React, Babel, and Webpack dependencies and helps automate the build process through scripts like npm run build.
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read