JavaScript Basics

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.js

What You Need to Get Started

Requirements (You Already Have These!)

Any modern web browser (Chrome, Firefox, Safari, Edge)
A text editor (even Notepad works!)
Basic understanding of HTML (helpful but not required)

Optional but Recommended

Visual Studio Code (free code editor)
Node.js (for running JavaScript outside browser)
Browser developer tools knowledge

Where JavaScript Runs

1. Web Browser

The most common way to run JavaScript. Every web browser has a built-in JavaScript engine.

Best for: Web development, interactive websites
Access: Browser console (F12)
Examples: Form validation, animations, user interactions

2. Node.js

Run JavaScript on your computer outside the browser. Great for server-side programming.

Best for: Server applications, command-line tools
Access: Terminal/Command Prompt
Examples: Web servers, file processing, APIs

3. Online Editors

Start coding immediately without installing anything. Perfect for learning and experimentation.

Best for: Learning, quick prototypes
Access: Any web browser
Examples: CodePen, JSFiddle, Repl.it

3 Ways to Start Coding JavaScript Right Now

1

Browser Console

Fastest way to start!

  1. Press F12 (or Cmd+Option+I on Mac)
  2. Click "Console" tab
  3. Type: alert("Hello World!")
  4. Press Enter

Perfect for testing small code snippets!

2

HTML File

Best for creating complete web pages!

  1. Create a new file: index.html
  2. Add HTML structure
  3. Include JavaScript in <script> tags
  4. Open file in browser

Great for building actual web projects!

3

Online Editor

Zero setup required!

  1. Visit CodePen.io or JSFiddle.net
  2. Click "Create New Pen/Fiddle"
  3. Write JavaScript in the JS panel
  4. See results instantly

Perfect for sharing and collaboration!

Recommended Development Tools

Code Editors

Visual Studio Code (Recommended)

Free, powerful editor with excellent JavaScript support

• IntelliSense autocompletion
• Built-in debugging
• Integrated terminal
• Massive extension library

Other Great Options

Sublime Text: Fast and lightweight
WebStorm: Feature-rich (paid)
Atom: Highly customizable
Vim/Emacs: For advanced users

Browser Tools

Developer Tools (Built-in)

Every browser includes powerful debugging tools

• Console for testing code
• Debugger for finding issues
• Network tab for monitoring requests
• Elements tab for DOM inspection

Keyboard Shortcuts

Windows/Linux: F12 or Ctrl+Shift+I
Mac: Cmd+Option+I
Console only: Ctrl+Shift+J (Win/Linux)
Console only: Cmd+Option+J (Mac)

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

2

Code Editor

Choose development tools

3

Runtime Environments

Browser vs Node.js vs Online

4

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)

• Make sure your HTML file is saved with .html extension
• Verify JavaScript syntax is correct
• Check that element IDs match your JavaScript code

"Script not loading"

Solution: Check file paths and names

• Ensure script.js is in the same folder as index.html
• Check spelling of file names
• Use browser developer tools to check for 404 errors

"Console shows undefined"

This is normal! Many statements return undefined

• console.log() returns undefined after printing
• 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

• Save your files before refreshing
• Use Ctrl+Shift+R for hard refresh
• Some editors auto-save, but double-check

Running JavaScript: summary

Key points

  • defer downloads the script without blocking the page and runs it after the HTML is parsed.
  • Use an external .js file 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.