Hello, World in JavaScript
Start quickly in the browser or with Node.js. JavaScript runs top‑to‑bottom; no special main function is required.
Browser (script tag)
Include a script and log to the console. Scripts in <body> execute when encountered; placing them at the end avoids blocking rendering.
<!doctype html>
<html>
<body>
<h1>Hello</h1>
<script>
console.log("Hello, world!")
// alert("Hello, world!") // modal popup (optional)
</script>
</body>
</html>
Open DevTools (Console) to see logs.
Browser modules (ESM)
Use <script type="module"> for modern module features. Module scripts are deferred by default and have their own scope.
<script type="module">
import { sum } from "./math.js"
console.log(sum(2, 3))
</script>
Node.js (CLI)
Create a file and run it with Node.
// hello.js
console.log("Hello, world!")
Run: node hello.js
ES modules vs CommonJS
- ESM (recommended): set
"type": "module"inpackage.jsonor use.mjs - CJS (legacy):
require()/module.exportsTop‑level await is supported in ESM in modern Node and browsers.
Console API and formatting
console.log("x=%d", 42)
console.info("info")
console.warn("warn")
console.error("error")
console.table([{ a: 1 }, { a: 2 }])
Template literals support interpolation.
const name = "JS"
console.log(`Hello, ${name}`)
Strict mode (safety)
Enable stricter semantics; prevents accidental globals and throws on some silent errors. ESM is strict by default.
'use strict'
Summary
- Use
<script>or Node to run code - Prefer ESM (
type: module); use DevTools console for quick tests console.*for logging; template literals for formatting