JSX
JSX is the acronym for JavaScript eXtensible Markup Language.
A syntax extension for JavaScript that lets developers write HTML-like code within JavaScript. It’s most commonly used with React, a popular JavaScript library for building user interfaces.
Here’s a breakdown of what JSX is and why it’s useful:
What JSX Looks Like
Instead of writing UI code like this:
React.createElement('h1', null, 'Hello, world!');
You can write:
<h1>Hello, world!</h1>
This JSX code gets transpiled (converted) into standard JavaScript by tools like Babel before running in the browser.
Why JSX Is Useful
- Visual Clarity: Writing HTML-like code makes the UI structure more readable and familiar to web developers.
- Component-Friendly: JSX enables React components to be written in a declarative way, which makes code easier to maintain and understand.
- Full JavaScript Power: You can embed JavaScript expressions inside JSX using curly braces (
{}
), such as{user.name}
or{items.map(...)}
.
Example
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
This function is a React component that returns a JSX element. It looks like HTML but is really JavaScript under the hood.
Summary
JSX isn’t required to use React, but it makes building interfaces faster and more intuitive. It’s what allows React developers to combine markup and logic in the same file.