.JS
JavaScript source code
Introduction
A .js file holds JavaScript, the programming language of web pages, as
plain text. Browsers run it in pages, and runtimes such as Node.js, Deno and Bun run it
on servers and in the terminal.
Two other extensions say which module system a file uses in Node.js:
.mjs for an ECMAScript module (import and
export) and .cjs for a CommonJS module
(require and module.exports).
Software
- Google Chrome, Firefox, Safari and every other web browser
- Node.js (Windows, macOS, Linux)
- Deno (Windows, macOS, Linux)
- Bun (Windows, macOS, Linux)
- Visual Studio Code (Windows, macOS, Linux)
- WebStorm (Windows, macOS, Linux)
Structure
A JavaScript file is plain text, normally UTF-8, with no header. A file is either a
classic script, whose top-level names become globals, or a module, which has its own
scope and shares names only through import and export. The
text alone does not always say which: a web page decides with
<script type="module">, and Node.js decides from the extension or the
"type" field of the nearest package.json.
import { readFile } from 'node:fs/promises';
export async function wordCount(path) {
const text = await readFile(path, 'utf8');
return text.split(/\s+/).length;
}