In JSX, how is a self-closing tag written?

Master ReactJS with our comprehensive test. Practice with multiple-choice questions and detailed explanations. Build your skills for the exam with our engaging format and expert tips!

Multiple Choice

In JSX, how is a self-closing tag written?

Explanation:
Self-closing in JSX uses a slash to show the element has no children. This XML-like syntax keeps empty elements explicit and ensures JSX compiles to React.createElement calls correctly. The typical line break tag is written as <br /> or <br/>, with the slash signaling that the tag closes itself. Writing <br> alone would be treated as an opening tag that needs a closing tag, which isn’t appropriate for an empty element in JSX. Another valid form is <br></br>, but the concise self-closing variant is the norm.

Self-closing in JSX uses a slash to show the element has no children. This XML-like syntax keeps empty elements explicit and ensures JSX compiles to React.createElement calls correctly. The typical line break tag is written as
or
, with the slash signaling that the tag closes itself. Writing
alone would be treated as an opening tag that needs a closing tag, which isn’t appropriate for an empty element in JSX. Another valid form is

, but the concise self-closing variant is the norm.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy