Loading...

JavaScript essentialsGetting Started

Introduction to JavaScript

5 min lesson

Next
  • JavaScript is a programming language that runs inside your web browser and makes web pages interactive, like buttons that respond when you click them.
  • It matters because almost every modern website uses JavaScript, and it is the only language that browsers understand natively, so learning it lets you bring pages to life.
  • Think of a web page as a puppet: HTML is the puppet's body, CSS is its costume, and JavaScript is the strings that make it move.
  • A program is a list of instructions the computer follows, one by one, like a recipe. JavaScript programs are called scripts and are stored in plain text files.
  • The computer reads your script from top to bottom and runs each instruction in order. It does not skip lines or guess what you meant.
  • A function is a named block of code that does a job. A statement is a single instruction, like 'show this message' or 'add these numbers'.
  • The basic form is a function call: console.log("Hello"); which tells the browser to print a message.
  • Real websites use JavaScript for shopping carts, chat boxes, live search suggestions and games.
  • A surprising detail: JavaScript is not the same as Java. They are two completely different languages that happen to share part of a name.
  • The example program prints a friendly greeting to the console, and you will see that message appear on screen.
Keep in mind
  • JavaScript makes web pages interactive and runs inside the browser.
  • Scripts run from top to bottom, one instruction at a time.
  • JavaScript and Java are different languages, so do not mix them up.
  • Everyone starts with small scripts. Try one line at a time and you will learn quickly.
hello.js
// This program prints a greeting to the console.
// A console is a text panel where programs show messages.

// Call the built-in console.log function to show text on screen.
console.log("Hello, world!");

// Print a second line so you can see the program runs top to bottom.
console.log("I am learning JavaScript.");

What happens: Hello, world! I am learning JavaScript.

Breaking it down

Comment line

// This program prints a greeting

A comment is a note for humans. The computer skips it completely. Comments explain why the code exists and make it easier to read later.

console.log call

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

This tells JavaScript to print the text inside the quotes to the console. The semicolon ends the statement, like a full stop ends a sentence.

String

"I am learning JavaScript."

Text inside double quotes is called a string. It is data the program can print or store. Without the quotes, JavaScript would think it was a name and look for a variable.

A helpful analogy

A web page is like a puppet show. HTML gives the puppet its shape and parts, CSS gives it clothes and colours, and JavaScript is the puppeteer's hands pulling the strings so the puppet moves and reacts. Without JavaScript the puppet just sits there.

Nesting rules

  • 1.Always put each statement on its own line so you can read the order clearly.
  • 2.Put text inside quotes when you want it treated as a string, for example console.log("hi");
  • 3.End every statement with a semicolon so the computer knows where it stops.

Common mistakes

Forgetting the quotes around text

Avoid

console.log(Hello); // error: Hello is not defined

Use instead

console.log("Hello"); // prints Hello

Without quotes JavaScript treats Hello as a variable name and complains it does not exist. Text needs quotes.

Confusing JavaScript with Java

Avoid

Looking for a Java tutorial to write a browser script

Use instead

Searching for a JavaScript tutorial instead

They are different languages with different rules. Java code will not run in a browser as a script.

Quick tips

  • •Change the text inside the quotes and run the script again to see how output changes.
  • •Read the console message carefully when something breaks; it often names the line.
  • •Keep your first scripts short and run them often so you see results fast.
Next