Writing Your First
JavaScript Program



Introduction

Welcome to JavaScript! In this lesson, you will write your first JavaScript program. We’ll begin with simple instructions that run in the browser console or Node.js, and build your confidence with real, working examples.

How to Run JavaScript

Before you start, you can run JavaScript in two common ways:

Example 1: Hello World

The classic first program in any language prints a message to the screen. In JavaScript, we use console.log() to output messages.

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

Output:

Hello, World!

How does it work?

The console.log() function sends output to the browser's console or the terminal in Node.js. This is extremely useful for debugging and seeing what your program is doing.

Question:

Can you print numbers or variables too?

Answer: Yes! You can print anything: strings, numbers, variables, objects, arrays, etc.

Example 2: Printing Numbers

console.log(42);
console.log(3.14);

Output:

42
3.14

Example 3: Declaring and Using Variables

JavaScript supports variable declarations using let, const, and var. Let’s create a variable and print it.

let name = "Alice";
console.log("Hello, " + name + "!");

Output:

Hello, Alice!

Example 4: Simple Calculation

Let’s perform a basic addition and print the result:

let a = 5;
let b = 7;
let sum = a + b;
console.log("The sum is:", sum);

Output:

The sum is: 12

Question:

What happens if you use const instead of let?

Answer: const creates a constant variable. Once assigned, its value cannot be changed.

Example 5: Constant Declaration

const pi = 3.14159;
console.log("The value of pi is", pi);

Output:

The value of pi is 3.14159

Tips for Beginners

Conclusion

You’ve written your first JavaScript program! You've learned how to print text and numbers, use variables, and even perform simple calculations. This is your foundation. In the next lessons, we’ll explore data types, operators, and control flow to build your programming skills further.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M