JavaScript Basics: Make Web Pages Come Alive · Lesson 1 of 8

Lesson 1: What JavaScript Is and Your First Program

What JavaScript does on the web, how to run it in your browser, and your first console.log().

Lesson 1: What JavaScript Is and Your First Program

A web page is built from three layers. HTML is the structure (headings, paragraphs, images). CSS is the style (colours, layout). JavaScript is the behaviour: what happens when you click, type or scroll. Almost every website you use has JavaScript running in it.

Where to run it

You do not need to install anything. Every browser has a console: press F12 (or right-click, then Inspect) and open the Console tab. You can type JavaScript there and press Enter. Later we will put JavaScript inside a web page.

Your first program

console.log() prints a value. Text goes in quotes. Statements usually end with a semicolon ;.

console.log("Namaste, world!");
console.log("I am learning JavaScript");

Output:

Namaste, world!
I am learning JavaScript

Comments

// This is a comment: the browser ignores it
console.log("Hello"); // and so is this note
/* A longer note
   over several lines */

Output:

Hello

Doing maths

console.log(5 + 3);
console.log(10 / 4);
console.log(2 * (3 + 4));

Output:

8
2.5
14

JavaScript is case sensitive: console.log works, Console.Log does not. If something fails, check spelling, quotes and brackets first.

Try it yourself

  1. Open your browser console and print your name.
  2. Calculate how many minutes are in a week.
  3. Break the code on purpose and read the error message in red.

Next lesson: storing values in variables.

Finished this lesson?Log in to save your progress and pick up where you left off.
Next lesson →

Comments

Loading comments…

MeroSir Team
A free MeroSir course. Run the examples yourself and try the exercises: you learn programming by doing. Spotted a mistake? Use the Report button above the lesson.