DEV Community

Saqib
Saqib

Posted on

2

What is JSX?

JSX looks similar to HTML, but it is not HTML. It is a syntax extension for JavaScript, which means it gets compiled into regular JavaScript code by a transpiler(Babel) before executing it in the browser.

It was developed by Facebook as a way to make it easier for developers to build user interfaces (UI) using React, a JavaScript library for building user interfaces.

const element = (
  <h1 className='greeting'>
   Hello JSX
  </h1>
)
Enter fullscreen mode Exit fullscreen mode

The above JSX is equivalent to the following JavaScript code,

const element = React.createElement(
 'h1',
 { className: 'greeting' },
 'Hello JSX'}
)
Enter fullscreen mode Exit fullscreen mode

Babel which converts our JSX to React.createElement()

Babel transpiler

As you can see above image shows how JSX get converted into React.createElement(). You can learn more about babel on their official documentation.

Embedding JavaScript Expression in JSX

JSX allows you to use JavaScript expression. you can use curly braces to include JavaScript expressions in your JSX code.

const name = 'Saqib';
const element = <h1>Hello, {name}</h1>;
Enter fullscreen mode Exit fullscreen mode

You can use any valid JavaScript expressions inside the curly braces in your JSX code. For example, 1 + 3, user.firstName all are valid javascript expressions.

function formatName(user) {
  return user.firstName + ' ' + user.lastName;
}

const user = {
  firstName: 'Mohammad',
  lastName: 'Saqib'
};

const element = (
  <h1>
    Hello, {formatName(user)}!
  </h1>
);
Enter fullscreen mode Exit fullscreen mode

That's it

Thank you for reading this article. Hope you like it. You can connect with me on Twitter

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay