Online Compiler logoOnline Compiler

JavaScript Tutorial

Writing Your First JavaScript Program — From Hello World to Real Code

Your first JavaScript program teaches the full input → logic → output flow.

Start simple with console.log, then expand into variables, conditions, and reusable logic.

Why We Need It

This first program builds confidence and the mental model for how JavaScript executes.

It also sets the foundation for functions, loops, and real-world apps.

Syntax

console.log("Hello, World!");
const name = "Asha";
if (name) { console.log(name); }

Basic Example

1. Classic Hello World

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

console.log() prints text to the console. This is your first output.

Real World Example

2. Running in Browser Console

console.log("Hello from the console!");

const message = "Test";
console.log(message);

Use DevTools Console for instant feedback while learning.

Multiple Use Cases

What Is a JavaScript Program?

A JavaScript program is a sequence of instructions executed by the JavaScript engine.

Programs follow a simple pattern: accept input → process logic → produce output.

Your first program will be simple, but it demonstrates all the fundamentals of how code execution works.

As you progress, programs become more complex by combining multiple concepts together.

The Simplest Program: Hello World

Traditionally, the first program prints "Hello, World!" to demonstrate that code can execute.

In JavaScript, console.log() is the built-in function for printing output.

console is an object, log() is a method, and the text in quotes is the content to print.

This single line demonstrates variables, strings, objects, methods, and output.

Method 1: Run Code in Browser Console

The fastest way to run JavaScript is directly in your browser's DevTools Console.

No files needed, no setup required - just open the console and type.

Perfect for learning, testing snippets, and debugging.

Every modern browser (Chrome, Firefox, Safari, Edge) has built-in DevTools.

Method 2: Create an HTML File with JavaScript

To run more complex programs, create an HTML file with embedded JavaScript.

The <script> tag contains your JavaScript code.

Opening the file in a browser executes the script.

Use DevTools Console to see console.log() output.

Method 3: External JavaScript File (Professional Approach)

Professional projects separate HTML and JavaScript into different files.

Create a separate .js file and link it in HTML using <script src="file.js"></script>.

This approach scales better for larger applications.

Makes code reusable and testing easier.

Method 4: Run JavaScript with Node.js

Node.js is a JavaScript runtime that executes JavaScript outside the browser.

Perfect for running scripts, building servers, and automating tasks.

Install Node.js, create a .js file, and run it with node filename.js.

Useful for backend development and command-line tools.

Building Your First Real Program

Combine what you've learned to create a program that actually does something.

Use variables to store data, console.log() to print output, and conditional logic to make decisions.

This program could greet a user, calculate values, or validate input.

How Code Execution Works

JavaScript executes code line by line from top to bottom.

Variables are created when declared and remembered for the rest of the program.

Each statement completes before the next one starts.

Understanding execution order is crucial for debugging and learning.

Common Beginner Mistakes

Typos in variable and function names cause errors.

Forgetting quotes around strings breaks the program.

JavaScript is case-sensitive - "name" and "Name" are different.

Mismatched parentheses or brackets prevent execution.

More Examples

3. External JavaScript File Structure

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

// script.js
console.log("External JavaScript file loaded!");

Separating files is the professional standard for maintainable projects.

4. Running JavaScript with Node.js

// app.js
console.log("Running in Node.js!");

const sum = 5 + 3;
console.log(`5 + 3 = ${sum}`);

// Run: node app.js

Node.js lets you run JavaScript outside the browser.

5. A Complete Simple Program

const userName = "Sarah";
const userAge = 17;
const currentYear = 2024;

const birthYear = currentYear - userAge;
const isAdult = userAge >= 18;

console.log(`Hello, ${userName}!`);
console.log(`Birth year: ${birthYear}`);
console.log(isAdult ? "Adult" : "Minor");

Combines variables, template literals, and conditions into one program.

Comparison

Without

// No output
const name = "Asha";

With

// With output
const name = "Asha";
console.log("Hello, " + name);

Common Mistakes and Fixes

Running complicated logic before testing basics

Start with console.log(), simple variables, and basic conditions. Then build up complexity.

Ignoring error messages

Error messages tell you exactly what's wrong. Read them carefully to fix issues quickly.

Not using console.log() for debugging

Print variable values to understand what's happening. Console.log is your best debugging friend.

Mixing JavaScript and HTML logic

Keep them separate for cleaner, more maintainable code.

Forgetting execution is sequential

Code runs top-to-bottom. Use the correct variable names after declaration.

Interview Questions

What does console.log do?

It prints output to the console so you can see program results.

What is the execution order in JavaScript?

Code runs top-to-bottom, one statement at a time.

What is a JavaScript program?

A sequence of instructions executed by the JavaScript engine.

Practice Problem

Practice: Print a greeting with a name and age.

const name = "Riya";
const age = 20;

// TODO: print greeting with name and age

One Possible Solution

const name = "Riya";
const age = 20;

console.log(`Hello, ${name}. You are ${age} years old.`);

Frequently Asked Questions

What's the difference between running code in console vs in a file?

Console is immediate and interactive for testing. Files persist code and are used for actual programs. Both execute the same way.

Can I run Node.js code in the browser?

Core JavaScript can run in both, but Node.js-specific APIs (file system, process) don't work in browsers.

What's the best way to learn: console, HTML, or Node.js?

Start with console for quick experimentation. Move to HTML files for interactive programs. Use Node.js once you understand basics.

Do I need to install anything to write JavaScript?

No. Browser console needs nothing. For Node.js, install from nodejs.org. For files, just use a text editor.

How do I debug if my program has an error?

Check the error message. Use console.log() to print variable values. Open DevTools console to see what's happening.

What comes after Hello World?

Learn variables, data types, operators, conditions, loops, functions, and objects - in that order.

Can I run multiple JavaScript files together?

In HTML, link multiple scripts. In Node.js, use require() or import. In console, each line is independent.

Is console.log() only for beginners?

No. Professional developers use console.log() constantly for debugging and monitoring programs.

Try It Yourself

Run the classic Hello World example and then change the text.