React vs JavaScript
Understand the relationship between JavaScript and React, how they differ, and when a developer might choose to use React for building an interactive user interface.
1. What Is the Difference?
JavaScript is a programming language used to add logic, interactivity, and behaviour to websites and applications.
React is a JavaScript library used to build user interfaces. It provides a component-based approach for describing the UI and managing updates when component data changes.
Therefore, React and JavaScript are not competing languages. React uses JavaScript, and a React application still relies on JavaScript for its logic.
2. Relationship Between JavaScript and React
This simplified diagram shows how JavaScript and React fit together when building a web application.
JavaScript
Programming language for logic, calculations, data handling, and browser behaviour.
React
JavaScript library for components, declarative UI, props, and state.
Web Application
JavaScript and React work together to provide application logic and an interactive user interface.
3. JavaScript vs React Comparison
| Aspect | JavaScript | React |
|---|---|---|
| What is it? | A programming language. | A JavaScript library for user interfaces. |
| Main purpose | Programming logic and application behaviour. | Building and rendering component-based interfaces. |
| DOM updates | Can update DOM elements directly using browser APIs. | React manages rendering and UI updates for the interface it controls. |
| Component system | Does not provide React's component model by itself. | Provides a component-based approach to UI development. |
| State management | Can store values in variables and objects. | Provides features such as state Hooks for reactive component updates. |
| Reusable UI | Can be organized using functions and modules. | Components are designed to be reusable UI units. |
| Required to learn React? | Yes. JavaScript fundamentals are essential. | Learn after understanding JavaScript fundamentals. |
| Typical use | Scripts, calculations, browser interactions, and application logic. | Interactive interfaces, dashboards, forms, and data-driven screens. |
4. Example Using Plain JavaScript
Suppose we want to show a welcome message when a user clicks a button. With plain JavaScript, we can select an HTML element, listen for a click, and update its text.
HTML
<button id="welcomeButton">
Show Welcome
</button>
<p id="message"></p>
JavaScript
const button = document.getElementById("welcomeButton");
const message = document.getElementById("message");
if (button && message) {
button.addEventListener("click", () => {
message.textContent =
"Welcome to CIIT Training Institute!";
});
}
Expected Behaviour
Before clicking
No welcome message is displayed.
After clicking
Welcome to CIIT Training Institute!
5. Example Using React
In React, the same kind of interaction can be implemented with component state. The UI is described using JSX, and React renders the current state value.
React Code
import { useState } from "react";
function WelcomeMessage() {
const [showMessage, setShowMessage] = useState(false);
return (
<div>
<button onClick={() => setShowMessage(true)}>
Show Welcome
</button>
{showMessage && (
<p>Welcome to CIIT Training Institute!</p>
)}
</div>
);
}
export default WelcomeMessage;
Expected Behaviour
Initial State
showMessage = false
Only the button is displayed.
After Clicking
showMessage = true
Welcome to CIIT Training Institute!
useState(false)sets the initial value.- The button click changes the state to
true. - The
&&condition controls whether the message is included in the UI. - React renders the component again to reflect the updated state.
6. Main Difference in the Examples
Plain JavaScript
- Select the HTML elements.
- Register the click handler.
- Manually update the message text.
React
- Store the message visibility in component state.
- Change state when the button is clicked.
- Describe the message using JSX and let React render it.
7. When Should You Use JavaScript or React?
Plain JavaScript May Be Suitable For
- Small scripts and calculations.
- Simple button and DOM interactions.
- Small pages with limited UI complexity.
- Tasks that do not need a full component-based UI structure.
React May Be Suitable For
- Large interactive user interfaces.
- Reusable course cards and shared components.
- Applications with changing data and state.
- Interfaces that benefit from component-based organization.
Summary
- JavaScript is a programming language.
- React is a JavaScript library for building user interfaces.
- JavaScript can manipulate the DOM directly through browser APIs.
- React provides components and declarative rendering for managing UI updates.
- Both approaches can build interactive websites.
- JavaScript fundamentals are essential for learning React.
- Choose an approach according to the application's complexity and requirements.