OH: It’s just JavaScript, right? I know JavaScript.
My coworker who will inevitably spend the rest of the day debugging an electron issue
“It’s Just JavaScript!” is probably a phrase you’ve heard before. I’ve used it myself a number of times.
It gets thrown around a lot, often to imply that a particular project is approachable because it can be achieved writing the same, ubiquitous, standardized scripting language we all know and love: JavaScript.
Take what you learned moving pixels around in a browser and apply that same language to running a server and querying a database. You can do both with the same language, It’s Just JavaScript!
But wait, what is JavaScript?
Is any code in a .js file “Just JavaScript”?
Let’s play a little game I shall call: “Is It JavaScript?”
Browser JavaScript
let el = document.querySelector("#root"); window.location = "https://jim-nielsen.com";That’s DOM stuff, i.e. browser APIs. Is it JavaScript?
“If it runs in the browser, it’s JavaScript” seems like a pretty good rule of thumb. But can you say “It’s Just JavaScript” if it only runs in the browser?
What about the inverse: code that won’t run in the browser but will run elsewhere?
Server JavaScript
const fs = require('fs'); const content = fs.readFileSync('./data.txt', 'utf8');That will run in Node — or something with Node compatibility, like Deno — but not in the browser.
Is it “Just JavaScript”?
Environment Variables
It’s very possible you’ve seen this in a .js file:
const apiUrl = process.env.API_URL;But that’s following a Node convention which means that particular .js file probably won’t work as expected in a browser but will on a server.
Is it “Just JavaScript” if executes but will only work as expected with special knowledge of runtime conventions?
JSX
What about this file MyComponent.js
function MyComponent() { const handleClick = () => {} return ( <Button onClick={handleClick}>Click me</Button> ) }That won’t run in a browser. It requires a compilation step to turn it into React.createElement(...) (or maybe even something else) which will run in a browser.
Or wait, that can also run on the server.
So it can run on a server or in the browser, but now requires a compilation step. Is it “Just JavaScript”?
Pragmas
What about this little nugget?
/** @jsx h */ import { h } from "preact"; const HelloWorld = () => <div>Hello</div>;These are magic comments which affect the interpretation and compilation of JavaScript code (Tom MacWright has an excellent article on the subject).
If code has magic comments that direct how it is compiled and subsequently executed, is it “Just JavaScript”?
TypeScript
What about:
const name: string = "Hello world";You see it everywhere and it seems almost synonymous with JavaScript, would you consider it “Just JavaScript”?
Imports
It’s very possible you’ve come across a .js file that looks like this at the top.
import icon from './icon.svg'; import data from './data.json'; import styles from './styles.css'; import foo from '~/foo.js'; import foo from 'bar:foo';But a lot of that syntax is non-standard (I’ve written about this topic previously in more detail) and requires some kind of compilation — is this “Just JavaScript”?
Vanilla
Here’s a .js file:
var foo = 'bar';I can run it here (in the browser).
I can run it there (on the server).
I can run it anywhere.
It requires no compiler, no magic syntax, no bundler, no transpiler, no runtime-specific syntax. It’ll run the same everywhere.
That seems like it is, in fact, Just JavaScript.
As Always, Context Is Everything
A lot of JavaScript you see every day is non-standard. Even though it might be rather ubiquitous — such as seeing processn.env.* — lots of JS code requires you to be “in the know” to understand how it’s actually working because it’s not following any part of the ECMAScript standard.
There are a few vital pieces of context you need in order to understand a .js file, such as:
- Which runtime will this execute in? The browser? Something server-side like Node, Deno, or Bun? Or perhaps something else like Cloudflare Workers?
- What tools are required to compile this code before it can be executed in the runtime? (vite, esbuild, webpack, rollup typescript, etc.)
- What frameworks are implicit in the code? e.g. are there non-standard globals like Deno.* or special keyword exports like export function getServerSideProps(){...}?
When somebody says, “It’s Just JavaScript” what would be more clear is to say “It’s Just JavaScript for…”, e.g.
- It’s just JavaScript for the browser
- It’s just JavaScript for Node
- It’s just JavaScript for Next.js
So what would you call JavaScript that can run in any of the above contexts?
Well, I suppose you would call that “Just JavaScript”.