Difference between .JS and .JSX
In this post, We can understand the difference between the JS and JSX. Before we start, what is mean by syntactic sugar?
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.
What is JSX?
JSX is an XML/HTML-like syntax used by React that extends ECMAScript so that XML/HTML-like text can co-exist with JavaScript/React code. The syntax is intended to be used by preprocessors (i.e., transpilers like Babel) to transform HTML-like text found in JavaScript files into standard JavaScript objects that a JavaScript engine will parse.
Basically, by using JSX you can write concise HTML/XML-like structures (e.g., DOM like tree structures) in the same file as you write JavaScript code, then Babel will transform these expressions into actual JavaScript code. Unlike the past, instead of putting JavaScript into HTML, JSX allows us to put HTML into JavaScript.
By using JSX one can write the following JSX/JavaScript code:
var nav = ( <ul id="nav"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Clients</a></li> <li><a href="#">Contact Us</a></li> </ul>);
And Babel will transform it into this:
var nav = React.createElement( "ul", { id: "nav" }, React.createElement( "li", null, React.createElement( "a", { href: "#" }, "Home" ) ),
React.createElement( "li", null, React.createElement( "a", { href: "#" }, "About" ) ),
React.createElement( "li", null, React.createElement( "a", { href: "#" }, "Clients" ) ),
React.createElement( "li", null, React.createElement( "a", { href: "#" }, "Contact Us" ) )
);
So that, JSX is a mixing of JavaScript and HTML to write a code more simple and elegant.
Difference Between JS and JSX:
JS is standard JavaScript, JSX is an HTML-like syntax that you can use with React to (theoretically) make it easier and more intuitive to create React components. As the docs say, the only purpose is to make it easier to create React components... there's not much else there.
Without JSX, creating large, nested HTML documents using JS syntax would be a large pain in the rear; JSX simply makes that process easier.
JSX should be used to create React Components?
And since react is just a library for JavaScript, it makes no difference for you to choose between JSX or JS. Your bundler/transpiler/whatever takes care of resolving what type of file contents there is. They’re completely interchangeable!
In some cases users/developers might also choose JSX over JS, because of code highlighting, but the most of the newer editors are also viewing the react syntax correctly in JS files.
As other mentioned JSX is not a standard JavaScript extension. It's better to name your entry point of Application based on .js and for the rest components, you can use .jsx.
There is one important reason to using .JSX for all component's file names. Actually, In a large scale project with huge bunch of code, if we set all React's component with .jsx extension, It'll be easier while navigating to different javascript files across the project(like helpers, middleware, etc.) and you know this is a React Component and not other types of the javascript file.
I hope, This post will helpful for you to better understanding about JS and JSX. Thank you and support me to do more posts.