To start writing JavaScript, create an HTML file, add a <script> element that loads a separate .js file, and open the page in any modern browser. You do not need to install anything. The browser's developer console also lets you run JavaScript immediately, without any file.
This guide shows the current way to set up a first project, write and run code, and avoid outdated habits such as var, inline onclick handlers and alert() for output.
The fastest way to run JavaScript is the developer console, which is built into Chrome, Edge, Firefox and Safari.
F12, or Ctrl+Shift+J on Windows and Linux, or Cmd+Option+J on macOS in Chrome.The console prints the result immediately. It is ideal for experiments, but nothing you type is saved.
For anything you want to keep, use two files in one folder: index.html and script.js. Create them in a code editor such as Visual Studio Code.
Open index.html by double-clicking it. Clicking the button writes the message into the page.
The defer attribute tells the browser to download the script while it parses the HTML and run it after the document is ready. That is why document.querySelector() finds the elements even though the script is in the <head>.
For a very small test you can put code between <script> tags in the HTML file. This works, but separate files are easier to cache, test and maintain.
A script without defer that sits at the top of the page runs before the elements below it exist, so it cannot find them yet. Either add defer, or place the script at the end of <body>.
Use const by default, let when a variable must be reassigned, and avoid var, which has function scope and surprising hoisting behaviour.
Template literals (backticks) insert values with ${}. Arrow functions give you short function expressions. Both are part of modern JavaScript and work in every current browser.
JavaScript also runs on servers and the command line through runtimes such as Node.js, Deno and Bun. With Node.js installed, save code in hello.js and run it from a terminal.
Use ES modules by writing import and export statements and loading the file with <script type="module" src="main.js"></script> in the browser. Module scripts are deferred automatically.
console.log() to inspect values and console.table() for arrays of objects.debugger; statement or set a breakpoint in the Sources tab to pause execution.alert() for output, because it blocks the page, and avoid document.write(), which is discouraged.No. Any modern browser runs JavaScript, and a plain text editor is enough. A code editor such as Visual Studio Code adds syntax highlighting and error checking.
<script> tag?Put it in the <head> with the defer attribute, or at the end of <body>. Both make sure the page elements exist before the script runs.
No. They are different languages with different syntax, runtimes and uses, despite the similar name.
var, let or const?Use const for values that do not change and let for values that do. Avoid var in new code.
The script ran before the HTML was parsed. Add the defer attribute, load the file as a module, or move the script to the end of <body>. Check the Console tab for null errors to confirm.
Start in the browser console for quick tests, then create index.html and script.js linked with <script src="script.js" defer>. Use const and let, add behaviour with addEventListener, and debug with the console.