Setup & Running JavaScript
Setting up JavaScript is simple - you already have everything you need! Learn how to run JavaScript in browsers, set up development tools, and write your first scripts.
How do I run JavaScript code?
The quickest way is the browser console: press F12 (or Cmd+Option+J on Mac), open the Console tab, type console.log("Hi") and press Enter. For a real page, put your code in a .js file and load it with <script src="app.js" defer></script>. To run JavaScript outside the browser, install Node.js and run node app.js.
<!-- index.html -->
<script src="app.js" defer></script>
// app.js
console.log("Hello from app.js");
# terminal (Node.js)
node app.jsWhat You Need to Get Started
Requirements (You Already Have These!)
Optional but Recommended
Where JavaScript Runs
1. Web Browser
The most common way to run JavaScript. Every web browser has a built-in JavaScript engine.
2. Node.js
Run JavaScript on your computer outside the browser. Great for server-side programming.
3. Online Editors
Start coding immediately without installing anything. Perfect for learning and experimentation.
3 Ways to Start Coding JavaScript Right Now
Browser Console
Fastest way to start!
- Press F12 (or Cmd+Option+I on Mac)
- Click "Console" tab
- Type:
alert("Hello World!") - Press Enter
Perfect for testing small code snippets!
HTML File
Best for creating complete web pages!
- Create a new file: index.html
- Add HTML structure
- Include JavaScript in <script> tags
- Open file in browser
Great for building actual web projects!
Online Editor
Zero setup required!
- Visit CodePen.io or JSFiddle.net
- Click "Create New Pen/Fiddle"
- Write JavaScript in the JS panel
- See results instantly
Perfect for sharing and collaboration!
Recommended Development Tools
Code Editors
Visual Studio Code (Recommended)
Free, powerful editor with excellent JavaScript support
Other Great Options
Browser Tools
Developer Tools (Built-in)
Every browser includes powerful debugging tools
Keyboard Shortcuts
Try It Now: First JavaScript Code
Here's a simple HTML page with JavaScript. Try running this code and experiment with it!
Code Editor
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My First JavaScript</title>
</head>
<body>
<h1>JavaScript Setup Example</h1>
<button id="clickBtn">Click Me!</button>
<div id="output"></div>
<script>
// JavaScript code goes here
console.log("JavaScript is running!");
document.getElementById('clickBtn').onclick = function() {
document.getElementById('output').innerHTML =
'<p>Button clicked! JavaScript works!</p>';
};
</script>
</body>
</html>Preview
Next Steps
Code Editor
Choose development tools
Runtime Environments
Browser vs Node.js vs Online
First Script
Write and run your code
No Installation Required!
JavaScript runs in every web browser, so you can start coding immediately without installing anything.
Try It Now: First JavaScript Code
Here's a simple HTML page with JavaScript. Try running this code and experiment with it!
Code Editor
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My First JavaScript</title>
</head>
<body>
<h1>JavaScript Setup Example</h1>
<button id="clickBtn">Click Me!</button>
<div id="output"></div>
<script>
// JavaScript code goes here
console.log("JavaScript is running!");
document.getElementById('clickBtn').onclick = function() {
document.getElementById('output').innerHTML =
'<p>Button clicked! JavaScript works!</p>';
};
</script>
</body>
</html>Preview
Step-by-Step: Your First JavaScript Project
Method 1: Inline JavaScript
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript</title>
</head>
<body>
<h1>Hello World!</h1>
<button onclick="alert('Hello!')">
Click Me!
</button>
<script>
console.log("JavaScript is working!");
</script>
</body>
</html>Save this as index.html and open it in your browser. The JavaScript code is embedded directly in the HTML.
Method 2: External JavaScript File
index.html
<!DOCTYPE html>
<html>
<head>
<title>External JS</title>
</head>
<body>
<h1>External JavaScript</h1>
<script src="script.js"></script>
</body>
</html>script.js
// This is your JavaScript file
console.log("Hello from external file!");
// Add interactivity
document.addEventListener('DOMContentLoaded', function() {
alert('Page loaded successfully!');
});Create both files in the same folder. External files keep your code organized and reusable.
Common Setup Issues & Solutions
"Nothing happens when I click"
Solution: Check the browser console for errors (F12)
• Verify JavaScript syntax is correct
• Check that element IDs match your JavaScript code
"Script not loading"
Solution: Check file paths and names
• Check spelling of file names
• Use browser developer tools to check for 404 errors
"Console shows undefined"
This is normal! Many statements return undefined
• Variable declarations return undefined
• This doesn't mean your code isn't working
"How to see my changes"
Refresh your browser! Ctrl+R or Cmd+R
• Use Ctrl+Shift+R for hard refresh
• Some editors auto-save, but double-check
Running JavaScript: summary
Key points
deferdownloads the script without blocking the page and runs it after the HTML is parsed.- Use an external
.jsfile rather than inline<script>code for anything more than a few lines. - The browser console shows errors with file names and line numbers. Check it first when something doesn't work.
- Online editors like the Compileum playground run HTML, CSS and JavaScript with a live preview and no setup.
Frequently asked questions
›Why is my JavaScript not running?
Open the console (F12) and look for a red error. Common causes are a wrong src path, the script running before the HTML exists (add defer), or a syntax error that stops the whole file.
›Do I need Node.js to learn JavaScript?
Not at the start. A browser is enough. You'll want Node.js later for tooling (npm, bundlers) and for server-side JavaScript.