JSX (JavaScript XML) is a syntax extension for JavaScript that allows developers to write HTML-like markup directly inside JavaScript files. Browsers cannot execute JSX natively; tools like Babel or SWC transpile JSX expressions into standard React.createElement() calls.
// JSX Code:
const element = <h1 className="title">Hello React!</h1>;
// Transpiled JavaScript Output:
const element = React.createElement('h1', { className: 'title' }, 'Hello React!');
<>...</>).<img>, <input>, and <br> must explicitly include a trailing slash (<img />).className instead of class, htmlFor instead of for).{}: Pass variables, mathematical calculations, and function calls inside {}.import React from 'react';
export default function UserProfile() {
const user = {
firstName: 'Jane',
lastName: 'Doe',
avatarUrl: 'https://via.placeholder.com/150',
isOnline: true,
};
const formatName = (u) => `${u.firstName} ${u.lastName}`;
return (
<div className="profile-card">
<img
src={user.avatarUrl}
alt={formatName(user)}
className="avatar"
/>
<h2>{formatName(user)}</h2>
<p className={user.isOnline ? 'status-online' : 'status-offline'}>
Status: {user.isOnline ? 'Active Now' : 'Offline'}
</p>
</div>
);
}
<div>{user}</div>) throws a runtime error (Objects are not valid as a React child). Access primitive properties explicitly (user.name).true, false, null, and undefined are valid children that render nothing to the DOM.Fix the syntax errors in this JSX snippet: <img src="pic.jpg"> <div class="box">Result: {10 + 20}</div>.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With