Meet JavaScript
Learn where JavaScript runs, how to load it, and how to inspect a result.
JavaScript is the language of behavior on the web. It can update a page, react to input, request data, and also run outside the browser on servers and command-line tools.
Start coding JavaScript
The quickest start is the browser console: open developer tools, select Console, type 2 + 2, and press Enter. For code you want to keep:
- Create
app.jsbesideindex.htmlin yourfirst-sitefolder. - Add the module script shown below just before the closing
bodytag. - Write JavaScript in
app.js, save both files, and refresh the page. - Read the first console error when something does not run.
<body>
<h1>My first JavaScript page</h1>
<script type="module" src="app.js"></script>
</body>
console.log("JavaScript is connected");
document.querySelector("h1").textContent = "JavaScript is running";
A first value
Open a browser console and pass a text value to console.log(). The console is a fast place to test an expression and inspect values.
console.log("Hello, Arcbyte!");
Load a module
Put JavaScript in its own file and load it near the end of the document or as a module. Modules are deferred automatically and keep variables out of the global scope.
<button id="hello">Say hello</button>
<script type="module" src="app.js"></script>
const button = document.querySelector("#hello");
button.addEventListener("click", () => console.log("Hello!"));
Where it runs
The JavaScript language is standardized, while each runtime provides extra APIs. Browsers provide the DOM, events, storage, and network APIs. Node.js provides APIs for files, servers, and processes.
Read an error
When code fails, start with the first console error. Its message, file name, and line number usually identify the earliest broken assumption.
Keep this
- JavaScript adds behavior to web pages.
- Modules are the default way to load application code.
- The same language can run in several environments.