How to Create a JavaScript File
To create a JavaScript file, open a plain-text or code editor, write your code, and save the file with a .js extension. For a web page, you also need an HTML file that loads it. This guide walks through a working browser example, then covers local testing servers, standalone scripts, and common saving mistakes.
What a JavaScript File Is
A JavaScript file is a plain-text file containing JavaScript code. You can create one with a code editor such as Visual Studio Code, or with a basic text editor such as Notepad.
Saving a JavaScript file and running it are separate tasks.
Saving creates the file on your computer. To execute its code, you need an environment that understands JavaScript:
- A web browser runs JavaScript loaded by an HTML page.
- Node.js runs JavaScript outside the browser, including command-line programs.
For a browser example, you do not need to install Node.js, Java, npm, or a framework. Your browser already has a JavaScript engine.
Keeping JavaScript in a separate file also makes it easier to organize your code and reuse it across pages.
How to Create and Run a JavaScript File
This example creates a web page with a button. Clicking the button changes a message, giving you a visible way to confirm that your JavaScript works.
Choose an editor and prepare it to save plain text before creating the files.
| Editor | What to Do Before Saving |
|---|---|
| A code editor, such as Visual Studio Code | Create a normal text file and save it with the required filename. UTF-8 is a suitable encoding. |
| Notepad on Windows | In File > Save As, select All files under Save as type so Notepad does not append a text-file extension. Choose UTF-8 encoding. |
| TextEdit on macOS | Save the file as plain text and use straight quotation marks rather than smart quotes in your code. |
Avoid word processors such as Microsoft Word: their document formats contain formatting information that does not belong in a source-code file.
Step 1: Create an HTML Page
- Create a folder named
js-demosomewhere convenient, such as your Desktop or Documents folder. - Open your editor and create a new file.
- Paste in this HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JavaScript File Test</title>
<script src="script.js" defer></script>
</head>
<body>
<h1>JavaScript File Test</h1>
<button id="run-demo" type="button">Run JavaScript</button>
<p id="message">Ready to test.</p>
</body>
</html>
- Save the HTML as index.html in the js-demo folder.
The <script> element tells the browser which external JavaScript file to load. The defer attribute makes the browser wait until it has parsed the HTML before executing this script, so the button and paragraph are available when the code runs.
Do not open the page yet, the referenced JavaScript file is the next file to create.
Step 2: Create the External JavaScript File
- Create another new file in your editor.
- Name it
script.jsand save it in the same directory as your HTML file, following MDN’s external JavaScript example. - Paste in this JavaScript:
const button = document.querySelector("#run-demo");
const message = document.querySelector("#message");
button.addEventListener("click", () => {
message.textContent = "Your JavaScript file is working!";
});
console.log("JavaScript loaded.");
- Save the file.
Put only JavaScript in this file. Do not include opening or closing <script> tags. Those tags belong in HTML.
Keep .js as the final filename extension. If your editor automatically appends .txt, the filename will no longer match the HTML reference.
If you are unsure what Windows saved, open File Explorer. On Windows 11, choose View > Show > File name extensions to display the full filename. Rename the file if it has an unwanted suffix.
The code finds the button and paragraph by their HTML IDs, then registers a click handler. The final line writes a diagnostic message to the browser’s Console; it does not display text on the page.
Step 3: Open the Page and Test the Button
- Open
index.htmlin your browser. If double-clicking opens an editor instead, right-click the file and choose a browser under Open with. - Confirm that the page displays the heading, button, and initial message.
- Click Run JavaScript.
The paragraph should change to:
> Your JavaScript file is working!
That visible change confirms that the browser loaded the external file and executed its click handler.
To try an edit, change the text inside the JavaScript assignment, save the file, reload the browser page, and click the button again. Saving updates the file on disk; reloading makes the browser load the updated code.
This example uses a classic external script with defer, so it can run directly from a local HTML file.
Run JavaScript in Other Setups
The basic file-creation process stays the same, but the way you load or execute the file depends on your project.
Test Through a Local Web Server
A local server is useful when you start using JavaScript modules, fetching other files, or testing features that behave differently under a local file:// URL. Browser module projects should be served over HTTP rather than opened directly from disk.
With Python 3 installed, you can serve your project without adding a JavaScript package.
- Open a terminal in the folder containing your HTML and JavaScript files.
- Start Python’s built-in HTTP server using the command for your system.
On Windows, with the Python launcher installed:
py -m http.server --bind 127.0.0.1
On macOS or Linux:
python3 -m http.server --bind 127.0.0.1
Python’s server uses port 8000 by default. The --bind option restricts the server to the loopback address, so it is accessible only from your computer.
- Leave the terminal running and open
http://127.0.0.1:8000/in your browser. - Click the page’s button and confirm that the message changes.
- When finished, press Ctrl+C in the terminal to stop the server.
If you see a directory listing instead of the page, check that the terminal is serving the correct folder and that your HTML file is named index.html.
If the terminal cannot find the Python launcher, Python must be installed or made available to the terminal before these commands can run. Python is not required merely to create a JavaScript file.
Run a Script Without an HTML Page
Use Node.js when you want to execute JavaScript as a command-line program rather than as part of a web page.
Browser objects such as document and window are not available in Node.js. A script that changes page elements therefore needs a browser, while a console-only example can run in Node.js.
With Node.js installed:
- Create a new plain-text file named
start.js. - Add this code and save the file:
const message = "Hello from JavaScript!";
console.log(message);
- Open a terminal in the folder containing the file.
- Run:
node start.js
The terminal should display Hello from JavaScript! and then return to the command prompt.
If the terminal reports that node is not recognized or cannot be found, install Node.js or correct its availability on your system’s command path. Reopen the terminal after installation, then run the command again.
Add JavaScript to an Existing Website
For a plain HTML website, create the JavaScript file in your local site project and deploy it along with the HTML.
The script’s src must match its location. A relative path is resolved from the HTML page’s URL, not from the location of your editor or terminal.
For example, if you move the JavaScript file into a js subfolder beside your HTML file, replace the existing script element with:
<script src="js/script.js" defer></script>
Save the HTML, upload or deploy both files while preserving that folder structure, and open the published page. Test the behavior the script is supposed to provide.
For this button example, the page must also contain the button and paragraph with the matching IDs. Loading the JavaScript alone does not create those elements.
For a CMS or framework project, use its supported asset and script-loading system. A CMS content editor may remove script tags, and a framework may expect JavaScript to enter through its build process rather than through a manually added HTML tag.
Check That Your JavaScript File Is Loading
If the page does not respond, first check whether the script loaded, then investigate what happened when it ran.
Right-click the page, choose Inspect, open the Console tab, and reload the page. For the browser example, a successful load prints JavaScript loaded.
Use the first relevant error to narrow down the problem.
| Symptom | Likely Cause | What to Check or Fix |
|---|---|---|
| The page displays, but there is no load message | The browser did not load the JavaScript file | Open the Network tab and reload. Inspect any failed script request, then compare its requested path with the actual file location and full filename. |
The Console reports Unexpected token '<' |
The JavaScript file contains HTML, or the server returned an HTML page instead of JavaScript | Inspect the script request’s response. Correct the URL if it returns an error page, or remove HTML markup from the source file. |
The Console reports that it cannot read properties of null |
A selector did not find an element, or the script ran before the element existed | Match the HTML IDs to the JavaScript selectors and ensure the external classic script uses defer. |
| The Console reports a syntax error | The code contains a typo, missing delimiter, or unsuitable quotation mark | Open the reported file and line. Check brackets, parentheses, and straight quotation marks, including the line immediately before the error. |
| Your saved changes do not appear | The browser has an older copy, or you edited a different file | Confirm the file was saved, inspect the loaded script in DevTools, and reload. If needed, enable Disable cache in the Network panel while DevTools is open. |
Filename capitalization also matters when you deploy. A path that works on a case-insensitive local filesystem can fail on a case-sensitive server, so keep the spelling and capitalization identical.
Once the Console is clear and the button responds, replace the demonstration behavior with your own code. Make one change at a time, save it, and reload the page so you can quickly identify what caused a new error.
Frequently Asked Questions
Can I Create a JavaScript File in Chrome Developer Tools?
Chrome DevTools lets you write reusable snippets, but a snippet is not automatically a .js file in your project folder. For a normal project file, create and save it in a text editor, then load it through HTML. Typing JavaScript into the Console executes it in the current page; it does not create a source file on your computer.
How Do I Create a JavaScript File at D:\start.js?
You can save a plain-text file to D:\start.js if your Windows computer has a writable D: drive. In a code editor, create a new file, add your JavaScript, choose File > Save As, and enter that full path as the filename. After saving, confirm that the file appears in the root of the D: drive with the .js extension.
Is JavaScript Still Used in 2026?
Yes. JavaScript remains a core language for browser-based interactivity, and it is also widely used outside browsers through environments such as Node.js. Creating and loading a plain JavaScript file is still a useful foundation, even when larger projects use frameworks or build tools.
How Can I Convert a JavaScript File to PDF?
You can print a JavaScript file’s source code to PDF, but the PDF will not execute the JavaScript. Open the JavaScript file in an editor that supports printing and choose File > Print. Select your system’s Save as PDF option or Microsoft Print to PDF and save the output. If you want a PDF of the page the script produces, run the web page first and print the rendered page instead.
How do I create a JavaScript file on a laptop or Mac for a website?
Create a plain-text file named script.js in a text or code editor and save it in the same folder as your HTML page. This works on a Mac as well, where an editor can save both the HTML and JavaScript files.
