ArticleslgStudy

computer science

JSX (programming)

JSX (programming) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand JSX (programming) rather than just read about it. In short: JSX is an XML-like extension to the JavaScript language syntax. Initially created by Facebook for use with React, JSX has been adopted by multiple web frameworks.

Key takeaways

  • JSX (programming) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect JSX (programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of JSX (programming) from memory before moving on to harder problems.

Reference excerpt

JSX is an XML-like extension to the JavaScript language syntax. Initially created by Facebook for use with React, JSX has been adopted by multiple web frameworks. Being syntactic sugar, JSX is generally transpiled into nested JavaScript function calls structurally similar to the original JSX. When used with TypeScript, the file extension is .tsx.

Process Code written in JSX requires conversion with a tool called a "transpiler", e.g. Babel, SWC, or Oxc, before it can be understood by web browsers. This processing is generally performed during a software build process before the application is deployed. The transpiler must make assumptions about the nature of the JSX use, otherwise it would not know what to convert JSX tags into. In the case of Babel (below), we use it in a mode which assumes that the tags are to be used with React, so it converts JSX tags into function calls to the React JSX Runtime, which return values corresponding to the internal representation of these tags. @babel/plugin-transform-react-jsx, which Babel uses for JSX, provides some flexibility for adapting to other platforms by allowing specifying alternative pragmas that take the role of React.createElement and React.Fragment.

Markup The following examples include both the React-JSX/TSX code and the transpiled output from Babel.

Basic example

The /*#__PURE__*/ comment added by Babel indicates that the tagged call is to a pure function, which is relevant for later steps such as tree shaking. Because it is not necessary for the functioning of the code, following examples have this annotation removed for ease of reading. The above transpiler output indicates an output from React 17+. In older versions the output would instead be a more straightforward:

The classic interface remains commonly used by non-React users of JSX.

Nested elements Multiple elements on the same level need to be wrapped in a single element such as the <div> element shown above, a fragment delineated by <Fragment> or in its shorthand form <>, or returned as an array.

Attributes JSX provides a range of element attributes designed to mirror those provided by HTML. Custom attributes can also be passed to the component. All attributes will be received by the component as props (properties). Valid JSX attribute names are valid JSX identifiers (variable names), which differ from ECMAScript identifiers mainly by additionally allowing the - (hyphen-minus) character in a non-starting position. This allows HTML attributes with dashes such as data-* and aria-role to be used easily. Attributes still with invalid characters may be notated with the {... spread} syntax.

Custom components Custom "tags" (components) are simply JavaScript functions that accept a specific calling style.

Here name="Connor" simply becomes an option { name: "Connor" }. Components that contain wrap components can be defined using the children attribute that we've already seen in the transpiled output:

Valid JSX tag names are valid JSX identifiers (variable names), which mean that hypenated XML and HTML tags can be used without issue. However, custom components cannot have hyphenated names due to limitation from ECMAScript grammar.

JavaScript expressions JavaScript expressions (but not statements) can be used inside JSX with curly brackets {}:

The example above will render:

Conditional expressions If–else statements cannot be used inside JSX but conditional expressions can be used instead. The example below will render { i === 1 ? 'true' : 'false' } as the string 'true' because i is equal to 1.

The above will render:

Functions and JSX can be used in conditionals:

The above will render:

Babel: pragmas Transpilers such as Babel offer the ability to target function calls other than those from React. Here Babel is directed to call tag and frag instead:

With different versions of tag and frag, different representations of the input can be produced:

Dialects The above covers the conversion from JSX to ECMAScript code. As indicated in the "pragma" section, the ECMAScript code that receives JSX-derived objects are free to do whatever it wants to these data:

Frontend applications such as React generally translate these data structures into function calls that manipulate the Document Object Model of a page. These libraries also have server-side rendering variants that, via manipulating a "browserless" DOM, produces an ordinary HTML or XML webpage by serializing the DOM. Backend applications may opt to generate HTML or XML directly by piecing together strings, effectively acting as a template engine. More niche applications (demonstrated above) may not have anything to do with HTML or XML entirely. As far as HTML and XML are involved, the most common areas of divergence are the handling of attributes and the setup of available components.

Attributes

Names Although JSX defines how attributes should be parsed – and Babel maintains a convention about how it should be conveyed to other ECMAScript code – neither define how these attributes map to actual HTML and XML attributes. For example, React uses the following convention to minimize the need for the {... spread} syntax:

Plain alphanumerical attribute names are unchanged. Hyphenated attribute names from HTML are converted to camelCase, with the exception of data-* and aria-*. This is in line with the JavaScript DOM API. The xlink:* attribute names are converted to camelCase, e.g. xink:href becomes xlinkHref. React's DOM renderer has a preset list of attributes that it knows about. Some of these are for conversion to HTML/XML, others are for its internal use. In the production (non-development) mode, only these attributes are allowed on output tags unless the tag is "svg" or "math".

Values The HTML style attribute applies a collection of CSS styles to the element with it. As an ergonomic measure, React-DOM not only accepts passing this value in string form, but also as a JavaScript object. Instead of <div style={"color:" + clr}>, one could write <div style={{ color: clr }}>. The implementation of the "style" function automatically translates the object to the correct string format. A similar camelcase-renaming operation is done to simplify the use of styles with hyphenated names. React-DOM also allows passing JavaScript functions instead of strings as values for the event handler attributes.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with JSX (programming)

Start with the simplest possible case. Write down what JSX (programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to JSX (programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about JSX (programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of JSX (programming)

In research
JSX (programming) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses JSX (programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
JSX (programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2014 software, Facebook software, JavaScript, so understanding it makes those chapters shorter.
In everyday life
Look for JSX (programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “JSX (programming)” →

How to study JSX (programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what JSX (programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain JSX (programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is JSX (programming) in simple terms?

JSX is an XML-like extension to the JavaScript language syntax. Initially created by Facebook for use with React, JSX has been adopted by multiple web frameworks.

Why does JSX (programming) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study JSX (programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on JSX (programming).

Tags

  • 2014 software
  • Facebook software
  • JavaScript
  • Open formats
  • XML markup languages

Keep exploring