Posted by

in

The Power of JSX: Combining HTML and JavaScript

Posted by:

|

On:

|

Image designed from Canva Pro

We utilize JSX to create HTML-like code in JavaScript when we’re using React. It looks and feels like HTML that is married to JavaScript (because it is actually JavaScript).

JSX is helpful since it makes the component more readable.

If you have not read my previous posts about React yet, here are a few points for you:

  • You can only return one element at a time when you’re creating a component using JSX. This is why we use a parent element. For instance, <div>

Welcome.jsx

const Welcome= () => {
return (

<div>

<h1>Daily Bugle</h1>
<p>Welcome to the best news outlet there where we keep this creep Spiderman in check!</p>

</div>

);
};
  • If the JSX spans multiple lines, wrap it in parentheses for clarity just like the one above
  • If you’re only returning a single line, you can just add the JSX after the return statement to return to it

SpidermanPoetry.jsx

const SpidermanPoetry= () => {

return <p>I have no money. No parents left alive. Spidey has no home.</p>;
};
  • Since JavaScript is under the hood of React, we can also perform calculation in JSX

BudgetCalculations.jsx

const BudgetCalculations = () => {
return <h1>{5+5}</h1>;
};
  • Embedded expression must go in between the curly braces just like what we have above
  • Since JSX is JavaScript, it gives us the full power of JavaScript! You can also reference variables inside JSX

Welcome.jsx

const name= "Tony Stark";
const Welcome = () => {
return <h1>Welcome Home, {name}</h1>;
};
  • That means we can perform calculations, reference variable, or use any valid Javascript expression inside the curly braces

Welcome.jsx

const score= 15;
const Welcome = () => {
return <h1>{score * 10}</h1>;
};
  • Therefore, we use embedded expression to insert JavaScript and JSX in other JSX elements

Profile.jsx

const name = "Peter Parker";

const Profile = () => {
return <h1>{name}</h1>;
};
  • In JSX, you can use all the tags you already know from regular HTML, including <div>, <h1>, <p>, <footer>, etc.
  • A reminder that some elements in HTML are self-closing which means that they only have a single tage that ends with a slash /

MyPage.jsx

const MyPage = () => {
return (
<div>
<h1>Ducks are great</h1>
<p>I love ducks and bubble baths</p>
<img src="https://shorturl.at/lUn8R" alt="Batmetal Returns by Jack Blin" />
<br />
<input type="text" placeholder="Type here..." />
</div>
);
};
  • You can also store JSX inside a variable. This is really useful when you want to write JSX in some part of your code and then you want to refer to it somewhere else
  • You can also reference other JSX elements using a JSX expression with curly braces
  • And if you want to include multiple JSX expressions within one component, you need to use the {} for each expression
  • You can use embedded expressions to pass values into a JSX elements attributes

MyPage.jsx

const nameHeader = <h1>BatMetal Forever!</h1>;
const age = 37;
const url = "https://shorturl.at/lUn8R";

const MyPage = () => {
return (
<div>
{nameHeader}
<p>I love ducks and bubble baths!</p>
<p>I am {age} years old.</p>
<br />
<img src={url} alt="Batmetal Returns by Jack Blin" />
</div>
);
};

JSX is a unique syntax that blends JavaScript with HTML.

It as a bridge between the logic of JavaScript and the structure of HTML. With JSX, you can write HTML-like code directly in your JavaScript files, allowing you to describe the layout of your UI in a way that’s both visual and declarative. 

This makes your code more readable and it also simplifies the process of building dynamic web applications by embedding expressions and JavaScript logic right into the markup.

Reference: react.dev and my school notes 💋😝


  • We Outsourced Our Brains To AI…Now What?

    The article discusses the cognitive costs associated with increased reliance on AI tools and the internet, raising concerns about diminishing memory and critical thinking skills. It highlights the “Google Effect” and cognitive offloading, showing a correlation between AI dependence and decreased analytical abilities, particularly among younger users, while emphasizing the importance of maintaining cognitive engagement.

  • Hi besties! You don’t have to post everything about your life online.

    A raw yet insightful look at online oversharing, digital privacy, and how web design and content creation subtly shape your behavior. Learn why protecting your data and attention matters more than ever in an AI-curated world.

  • Hi, Humanity. Not Everything You Read Online Is Factual.

    Learn how to spot misinformation online by understanding SEO, AI bias, emotional manipulation, and the psychology behind digital content.

  • I Tried to Build an App…Now I’m in a Full Existential Spiral

    Choosing My Old App…and Accidentally Rebuilding a Whole Product So, I graduated in April 2025 with a glowing average of 95.8% (yes, mom, I know, why not A++? Just kidding) from UofT Continuing Studies – Software Development BootCamp, and instead of coding immediately like I probably should have, I sat there like: Hmm… what is…

  • Implementing Dark Mode in React

    We have previously talked about useState before. A great way to create CSS classes on the fly is to use state variable! Here is an example of how to create Dark Mode in React by using string interpolation to construct the individual classes based on the state variable mode. index.html index.jsx App.jsx App.css So, if…

  • Understanding React’s useState Hook

    Buttons To use buttons, you can add the onClick event handler to handle clicks on the button. React also follows the camelCase unlike HTML/JavaScript; and to run the JavaScript, you add an embedded expression. App.jsx You can also run JavaScript directly inside the embedded expression. However, it is executed without clicking the button because the function is…

  • Understanding Props in React: A Comprehensive Guide

    This content explains the concept of props in React, which allow data to be passed to components, making them dynamic. It covers how to access props as objects, pass multiple properties, and handle different data types. The importance of correctly rendering non-string values and using props in JSX is emphasized.

  • The Power of JSX: Combining HTML and JavaScript

    JSX is a syntax extension for JavaScript that resembles HTML, used primarily in React to create components. It allows developers to write HTML-like code within JavaScript, enhancing readability and enabling dynamic web applications. With JSX, one can embed JavaScript expressions, making the integration of logic and UI layout seamless.

  • Organizing React Components for Better Code

    The content discusses best practices for organizing React components in separate files for maintainability. It details the process of importing React, creating functional components, and exporting them. Additionally, it covers returning multiple elements and using named vs. default exports, emphasizing strategic organization for better code management.

  • Step-by-Step Guide to Rendering React Components

    The content provides a step-by-step guide on rendering components in React. It emphasizes creating and linking HTML and JavaScript files, setting up the main component, and utilizing the createRoot function to manage rendering. The author injects humor while guiding readers through the essential concepts of React component rendering.



Discover more from Steal My Notes

Subscribe to get the latest posts sent to your email.

Discover more from Steal My Notes Here

Subscribe now to keep reading and get access to the full archive.

Continue reading