DEV Community

Discussion on: JSX: In A Nutshell

Collapse
 
bawa_geek profile image
Lakh Bawa • Edited

I always wonder, why there is no need to put quotes or something around the JSX code. Javascript requires you to put quotes or double quotes around the string or HTML code like that.

return (

{text}

// will show JSX is cool

);
Collapse
 
pabloabc profile image
Pablo Berganza

That's because it's not a string. It's being parsed as JavaScript code and transformed into React.createElement. if you put quotes around JSX there would not be a way to tell the parser "hey, this should be transformed to React.createElement" since it would be parsed as a string.

Collapse
 
bawa_geek profile image
Lakh Bawa

Thanks for the reply, but there is no starting tag like <?php , how parser knows its jsx not html.

Is it true that , if there are no quotes, js compliler automaticly assumes its jsx code.?

Thread Thread
 
pabloabc profile image
Pablo Berganza

The same way a parser knows how fn() is a function call, or how if {...} is an if block! 😅

Don't see JSX as a templating language, see it as part of the JavaScript syntax! You don't need a tag to specify it is JSX because it already is "valid" JavaScript 😊

Aside note: actually the browser doesn't understand JSX at all. Babel is the parser that turns JSX into React.createElement, and that's what the browser sees (and what it can understand) in the end. The above mental model is approximately correct, just don't try to use JSX directly in the browser 😅

Thread Thread
 
bawa_geek profile image
Lakh Bawa

ahhh, got the point here; Don't see JSX as a templating language, see it as part of the JavaScript syntax! You don't need a tag to specify it is JSX because it already is "valid" JavaScript.

Thank you man, Thanks a lot

Thread Thread
 
pabloabc profile image
Pablo Berganza

Glad I could be of help!