Online Compiler logoOnline Compiler

JavaScript Tutorial

Writing Your First JavaScript Program (Beginner's Guide)

Writing your first JavaScript program is an exciting milestone. JavaScript makes websites interactive by handling user actions, updating content dynamically, validating forms, and much more.

Why this matters

This first program gives you the foundation for understanding logic, syntax, and runtime behavior before moving to advanced topics and frameworks.

What Is a JavaScript Program?

A JavaScript program is a set of instructions executed step by step by a browser or runtime environment.

At a basic level, every program takes input, processes logic, and produces output.

Step 1: Your First JavaScript Code

The classic first program is:

  • console.log("Hello, World!");
  • console is a built-in JavaScript object.
  • log() prints output.
  • "Hello, World!" is a string value.

Step 2: Run It in the Browser

Method 1 (fastest): use browser DevTools Console.

  • Open Chrome/Firefox.
  • Right click page -> Inspect.
  • Go to Console tab.
  • Run: console.log("Hello, World!");

Method 2: Run via HTML File

Create index.html and place JavaScript inside a script tag. Open in browser and check Console output.

Step 3: Use an External JavaScript File (Best Practice)

Professional codebases keep JavaScript separate from HTML for maintainability and scalability.

  • Create index.html
  • Create script.js
  • Link using <script src="script.js"></script>

Step 4: Run JavaScript Using Node.js

JavaScript can run outside browser using Node.js. Create app.js and execute it with node app.js in terminal.

Step 5: Understand Basic Concepts

Extend your first program with variables and text output.

  • let declares a variable
  • Strings are text values
  • Numbers are numeric values
  • + concatenates text

Step 6: Template Literals (Modern Approach)

Template literals use backticks and ${} interpolation to make strings more readable than repeated concatenation.

Step 7: User Interaction in Browser

Browser-only APIs like prompt() and alert() let you collect input and show UI popups.

Step 8: Add Conditional Logic

Use if...else to make decisions in code and produce different outputs based on conditions.

Common Beginner Mistakes

  • Missing quotation marks around strings
  • Using incorrect variable names
  • Forgetting JavaScript is case-sensitive
  • Writing Name instead of name
  • age is not equal to Age

Best Practices for Beginners

  • Use meaningful variable names
  • Keep code properly indented
  • Use console.log() for debugging
  • Start with small programs
  • Practice consistently

Mini Practice Exercises

  • Print your full name using console.log()
  • Create two numbers and print their sum
  • Ask the user for age and print whether they are adult (18+)

What You Have Achieved

You now understand how JavaScript executes code, prints output, declares variables, uses conditions, and runs in browser and Node.js.

This is the base for moving to real application development with React and Next.js.

Final Thoughts

Every advanced developer once started with console.log("Hello, World!").

Focus on understanding how code runs, not memorizing syntax only.

Code Examples

First Program

console.log("Hello, World!");

Your first JavaScript output statement.

Run in HTML

<!DOCTYPE html>
<html>
<head>
  <title>My First JavaScript Program</title>
</head>
<body>
  <h1>Learning JavaScript</h1>
  <script>
    console.log("Hello, World!");
  </script>
</body>
</html>

Place script in HTML and run it directly in browser.

External File Setup

<!-- index.html -->
<script src="script.js"></script>

// script.js
console.log("Hello from external JS file!");

Preferred structure for clean and scalable projects.

Run with Node.js

// app.js
console.log("Hello from Node!");

// terminal
node app.js

Executes JavaScript outside browser using Node runtime.

Variables + Template Literal + Condition

let name = "Chandan";
let age = 25;
let number = 10;

console.log(`My name is ${name} and I am ${age} years old.`);

if (number > 5) {
  console.log("Number is greater than 5");
} else {
  console.log("Number is 5 or less");
}

Combines beginner fundamentals into one small program.

Common Mistakes and Fixes

Not checking console errors

Always open Console and read errors line by line.

Case mismatch in variable names

Remember JavaScript is case-sensitive.

Skipping basics and jumping ahead

Practice small programs daily before advanced topics.

Frequently Asked Questions

Can I run JavaScript without installing anything?

Yes, run it directly in browser DevTools Console.

Why use external .js files?

They keep code modular, cleaner, and easier to maintain.

Can the same JavaScript run in Node.js?

Core JavaScript can run in both, but browser-specific APIs like alert/prompt do not work in Node.js.

What should I learn after this?

Move to variables, data types, operators, control flow, and functions.

Related JavaScript Topics