Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to access javascript in ReactJS

Getting Started with Accessing JavaScript in ReactJS

If you are starting your journey in programming, you might be wondering how JavaScript and ReactJS interact. Let's demystify this. ReactJS is actually a JavaScript library. This means that it's made up of JavaScript code. Therefore, to access JavaScript in ReactJS is to use JavaScript inside your ReactJS code.

Let's delve into some practical examples and analogies to make this concept clearer.

A Simple ReactJS Component

A ReactJS component is a reusable piece of code that returns a React element. This element is what your users will see and interact with on the screen. You can think of a React element like a single frame in a movie. It represents the UI at a certain point in time.

Here is a simple example of a ReactJS component:

function Greeting() {
  return <h1>Hello, world!</h1>;

In this code, the function Greeting is a React component that returns a React element (<h1>Hello, world!</h1>). This element is written using JSX.

What is JSX?

JSX stands for JavaScript XML. It is a syntax extension for JavaScript, and it is used with React to describe what the UI should look like. In simple terms, it is a way to write HTML style code in your JavaScript.

Here is the same Greeting component written with standard JavaScript:

function Greeting() {
  return React.createElement('h1', null, 'Hello, world!');

As you can see, JSX looks much cleaner and it is easier to understand, especially for beginners.

Accessing JavaScript in JSX

Now that we understand what JSX is, how do we access JavaScript in it? The answer is simple, you can use JavaScript expressions inside {} in JSX.

Here is an example:

function Greeting() {
  const name = 'John Doe';
  return <h1>Hello, {name}!</h1>;

In this example, {name} is a JavaScript expression. This expression will be evaluated to the name variable's value, which is 'John Doe'.

Think of inserting JavaScript in JSX like inserting a variable into a sentence in English. For instance, consider the sentence "Hello, my name is {name}!". Here, "{name}" is a placeholder for a person's name. Similarly, in JSX, "{name}" is a placeholder for the value of the name variable.

Conditional Rendering in JSX

Using JavaScript in JSX is not just limited to variables. You can also use conditions, loops and other JavaScript expressions.

Here is an example of conditional rendering, where a different React element will be returned based on a condition:

function Greeting({ isMorning }) {
  if (isMorning) {
    return <h1>Good morning!</h1>;
  } else {
    return <h1>Good evening!</h1>;

In this component, if the isMorning prop is true, "Good morning!" will be displayed. If it's false, "Good evening!" will be displayed.

Again, think of this like a conversation. If it's morning, you say "Good morning!". If it's not, you say "Good evening!".


Accessing JavaScript in ReactJS is like using the spices in your kitchen when you cook. Just as you would add a pinch of salt to bring out the flavor in your food, you can use JavaScript to add functionality and interactivity to your React components.

Whether it's simply displaying a variable, or managing more complex logic with conditions and loops, JavaScript expressions embedded in JSX provide you with the tools you need to create dynamic and interactive user interfaces.

Remember, practice makes perfect. The more you use JavaScript in your React components, the more comfortable you'll become with it. So, get your hands dirty, start coding and happy React-ing!