How Node.js Works
Node.js runs JavaScript outside the browser. It lets JavaScript code run on a computer like any other program.
Run JavaScript in Node
This line prints a greeting. Run the file with Node.
console.log("Hello from Node");A timer in Node
This timer prints Later after one second. Node waits in the background.
setTimeout(() => {
console.log("Later");
}, 1000);Example
console.log("Node runs JavaScript");
setTimeout(() => {
console.log("...outside the browser");
}, 0);Key points
- Node runs JavaScript code on a computer, not only in a browser.
- A browser is the program that shows web pages.
- Much of the same JavaScript runs in Node and in the browser.
- Node reads a file and runs its code from top to bottom.
- Node handles waiting work in the background while code keeps running.
- A timer is one kind of waiting work, like setTimeout.
In this example, the first print runs at once and shows Node runs JavaScript. The timer waits zero milliseconds, then prints the second line. Node keeps running while the timer waits.
Note
Code that needs a web page does not run in Node.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does Node.js do?