What JavaScript is and where it runs
JavaScript is a dynamically typed language that runs in every browser, and since 2009 outside one as well, through Node.js. A program is a text file with the extension .js, and the runtime reads it as it is: there is no separate build step between the file you write and the program that runs, and no header, no declarations and no main function.
This lesson is the tour before the work. It says what the language is, where it runs, what an event loop does in one paragraph, and it runs two short programs so that the lines on the page come from a real machine. Everything here is the beginning of the course: the ten lessons of this part take the same language from the first file to functions, one command at a time.
What JavaScript is
The language is standardized as ECMAScript, and every runtime implements that standard. Values carry the types, not the variables: let answer = 42; creates a number, and the same binding may hold a string on the next line. That is what dynamically typed means, and it is why a program fails at run time rather than at build time when a value has the wrong shape.
In a browser the runtime adds the page: the DOM, with its HTML, the styles and the events. Node.js adds the other side instead: files, processes, network sockets and a module system. The core of the language is the same in both, and that core is what this part teaches.
One rule decides how a program moves: the code you write runs to the end on one call stack before any callback runs. Timers, network answers and promises do not interrupt a running function; they queue.
Where JavaScript runs
The same file can be read by a browser, by Node.js and by newer runtimes such as Deno, and the differences show up in the objects around the language. process is a Node.js object and does not exist in a browser. window is the browser object and does not exist in Node.js. The typeof operator answers "undefined" for a name that has never been declared, so one line tells the two apart.
console.log('node:', process.version);
console.log('platform:', process.platform);
console.log('arch:', process.arch);
console.log('typeof window:', typeof window);
console.log('typeof document:', typeof document);
console.log('typeof globalThis:', typeof globalThis);
node runtime.js
node: v24.22.1
platform: linux
arch: x64
typeof window: undefined
typeof document: undefined
typeof globalThis: object
This course runs its programs with Node.js 24, so the first line is that version and window is reported as missing. Drop the same file into a page with a <script> tag and the last three lines change places: there window is an object and process is the one that is undefined.
The event loop
JavaScript has one call stack. Everything that runs runs on it, and a function runs to its last line before anything else starts. Work that would wait, such as a timer or a network answer, is handed to the runtime, which puts a callback in a queue when the work is done; the event loop takes the next callback from the queue and runs it on the stack when the stack is empty.
Promises add a second, shorter queue. A callback passed to then is a microtask, and every microtask runs before the next timer callback, however soon that timer was set. The program below prints four lines in an order that no line of the source suggests.
console.log('1: synchronous start');
setTimeout(() => {
console.log('4: timeout callback');
}, 0);
Promise.resolve().then(() => {
console.log('3: promise callback');
});
console.log('2: synchronous end');
node eventloop.js
1: synchronous start
2: synchronous end
3: promise callback
4: timeout callback
The two synchronous lines finish first, because the stack is not empty while they run. Then the microtask queue empties, which prints the promise callback, and only then does the timer callback run. The zero in setTimeout is a delay, not a promise to run at once.
The same language in a browser
A browser program needs a document, so the page cannot run the next fragment with node; it is here to show what the other runtime adds. The document object is the page, querySelector finds an element, and textContent replaces its text:
const heading = document.querySelector("h1");
heading.textContent = "Hello from JavaScript";
In a file served to a browser, every line of the Node.js programs still works the same way, and only the objects around them differ. The lessons on the browser and the DOM in the later part of the course come back to this fragment.
Exercises
- Run node runtime.js and read the three typeof lines. Which of the three names exists in both runtimes, and why is its type an object?
- Add a line with console.log(process.version) to eventloop.js and move it above the timer. Predict the new order of the five lines before you run the file.
- Change the delay in setTimeout from 0 to 100. Does the promise callback still print before the timer callback?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where can JavaScript run?
Answer
In browsers and, through Node.js, outside one
The language core is the same everywhere, and Node.js supplies files, processes and sockets instead of the page.
-
What does
typeof windowprint when the file is run withnode?Answer
undefined
There is no window object in Node.js, and typeof answers undefined for a name that was never declared.
-
Which object does Node.js add that a browser does not have?
Answer
process
process carries the version, the platform, the arguments and the exit code; the other three belong to the browser.
-
What does this program print?
console.log("1"); setTimeout(() => console.log("2"), 0); Promise.resolve().then(() => console.log("3")); console.log("4");Answer
1, 4, 3, 2
The two synchronous lines run first, then the microtask queue, then the timer callback with its zero delay.
-
What does
setTimeout(fn, 0)promise?Answer
That fn runs as soon as the call stack is empty and the timers queue reaches it
The delay is a minimum, and the timer callback waits in a queue behind the microtasks.
-
Which callbacks form the microtask queue?
Answer
Callbacks passed to Promise.then
A then callback is a microtask, and the microtask queue empties before the timers queue is touched again.
-
What does
typeof globalThisreport in Node.js?Answer
object
globalThis is the object that holds the global names, and its type is object in both runtimes.
-
Why is there no build step between the file and the program?
Answer
Because the engine compiles each function just in time, and the file itself is what the runtime reads
V8 compiles functions just in time, so the readable source file is the program you run.
-
What is ECMAScript?
Answer
The standard that defines the language
Every runtime implements the ECMAScript standard, which is why the same core works in a browser and on a server.
-
What does the browser fragment in this lesson need in order to run?
Answer
A page with an h1 element and a script tag
document.querySelector looks for an element in the page, and the browser supplies the document that Node.js does not have.
The code on this page was run with Node.js 24.22.1 and npm 11.12.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container. The browser fragment was not run: it needs a page.
Article author: Arthur Isaev