JavaScript files open in a text editor, not in a browser window
A JavaScript file is a plain text file with a .js extension. You cannot double-click it to run it the way you run a program — it needs either a text editor to view the code, or a web browser to execute it as part of a webpage. Most of the time, when you need to open a JavaScript file, you want to read or edit the code inside it, which means using a text editor.
The simplest approach is to right-click the file, choose "Open with", and pick a text editor from the list. On Windows, Notepad works but is bare-bones. On Mac, TextEdit works if you switch it to plain text mode first. For any serious work, a dedicated code editor like Visual Studio Code (free) or Notepad++ (free on Windows) makes the code much easier to read.
Key Takeaways
- Right-click a .js file and select "Open with" to choose a text editor instead of letting Windows or Mac pick a default program.
- Visual Studio Code and Notepad++ are free editors that color-code JavaScript syntax and make the file readable; Notepad and TextEdit work but show plain text.
- If you want to run the JavaScript code (not just read it), you need a web browser and an HTML file that loads the .js file with a script tag.
- Never double-click a .js file expecting it to run — it will either open in a text editor or try to open in your browser, and nothing will happen either way.
Opening a JavaScript file on Windows
Right-click the .js file and select "Open with". A menu appears showing programs on your computer. If you see Notepad, Notepad++, or Visual Studio Code, click it. The file opens and you see the code as text.
If you do not see a code editor in the list, click "Choose another app" or "More apps". Scroll down and look for Visual Studio Code if you have it installed. If you do not have a code editor yet, you can download Visual Studio Code free from microsoft.com — install it, then repeat the right-click and "Open with" step.
If you accidentally set a .js file to always open in Notepad or your browser, you can change it back. Right-click the file, select "Open with", then "Choose another app", then look for "Choose a default program for this file type" or similar wording at the bottom of the menu.
Opening a JavaScript file on Mac
Right-click (or Control-click) the .js file and select "Open With". A submenu shows available programs. Look for a code editor like Visual Studio Code or Sublime Text if you have one installed. Click it and the file opens.
If you only see TextEdit in the list, you can use it, but you must switch it to plain text mode first. Open the file with TextEdit, then go to Format menu and click "Make Plain Text". Otherwise TextEdit adds formatting that corrupts the code. After that, you can read the file, but editing and saving is awkward.
To set a code editor as the default for all .js files, right-click a .js file, select "Open With", find your editor, and look for an "Always Open With" or similar checkbox. This way, future .js files open in that editor automatically.
Using a code editor to view and edit JavaScript
Visual Studio Code is the most common free choice. Download it from code.visualstudio.com, install it, then open your .js file by dragging it into the Visual Studio Code window, or by using File > Open File inside the editor.
Code editors show the JavaScript in color-coded blocks — keywords appear in one color, text strings in another, comments in a third. This makes the code much easier to scan and understand. They also show line numbers on the left, which helps if you need to find a specific part of the code or report an error.
If you edit the file and want to save changes, press Ctrl+S (Windows) or Cmd+S (Mac). The editor saves the file as plain text, which is what JavaScript needs.
Running JavaScript code in a web browser
Opening a .js file in a text editor lets you read the code, but it does not run the code. To actually execute JavaScript, you need a web browser and an HTML file that loads the .js file.
Create a simple HTML file (or edit an existing one) and add this line inside the <body> tag:
<script src="yourfile.js"></script>
Replace "yourfile.js" with the actual name of your JavaScript file. Save the HTML file, then open it in your browser by double-clicking it or dragging it into a browser window. The browser reads the HTML, finds the script tag, loads the .js file, and runs the code.
If the JavaScript does not work as expected, open your browser's developer tools (press F12 or Ctrl+Shift+I on Windows, Cmd+Option+I on Mac) and look at the Console tab. Error messages there tell you what went wrong.
Checking what program is set to open .js files
On Windows, right-click a .js file and select Properties. Look for "Opens with" near the top. That shows which program is currently set as the default. To change it, click "Change" and pick a different program.
On Mac, right-click a .js file and select "Get Info". Look for "Open with" in the window that appears. Click the dropdown menu to see other options, or click "Change All" to set that program as the default for all .js files.
If you see your browser listed as the default, change it to a text editor. Browsers do not display .js files in a useful way — they either show the raw code in a confusing format or try to run it and fail silently.
Frequently Asked Questions
What happens if I double-click a .js file?
It opens in whatever program is set as the default for .js files on your computer. Usually that is Notepad, your browser, or a code editor you installed. The file does not run or execute — it just opens for viewing or editing. To actually run JavaScript code, you need an HTML file that loads it with a script tag.
Can I open a .js file in my browser?
Yes, but it is not useful. Drag the .js file into your browser window or use File > Open. The browser shows the raw code as plain text, which is hard to read and does not execute the code. To run JavaScript in a browser, create an HTML file with a script tag that points to your .js file, then open the HTML file.
Is Notepad good enough to edit JavaScript?
Notepad works for small edits, but it does not color-code the syntax or show line numbers, which makes mistakes hard to spot. For anything beyond a quick look, Visual Studio Code or Notepad++ is worth the free download. They catch common errors and make the code much easier to read.
Why does my .js file open in my browser instead of a text editor?
Your browser is set as the default program for .js files. Right-click the file, select "Open with", and choose a text editor instead. Then check the box to make that editor the default for all .js files going forward.
Do I need to install anything to open a .js file?
No. Notepad (Windows) and TextEdit (Mac) come built in and can open .js files. For a better experience, download Visual Studio Code free from microsoft.com. No other software is needed just to view or edit the code.