Skip to content
FORGENO ADS GAMESUnlock all $8

JavaScript · Unit 1: JavaScript basics · Lesson 1 of 5

Your first program

JavaScript gives web pages behavior.

Opens this lesson in Code Academy.

The idea

JavaScript gives web pages behavior. Here your programs run in a safe sandbox and show results with console.log(...).

Text in code is called a string and goes in quotes: "hello" or 'hello'. Numbers are written without quotes: 42.

Each statement ends with a semicolon, and code runs from top to bottom, one line after another.

Worked example

JavaScript

console.log("Hello, world!");
console.log(42);
console.log("2 + 2 is", 2 + 2);

It prints

Hello, world!
42
2 + 2 is 4

console.log can print several things at once, separated by commas. It puts a space between them.

Key terms

console.log
Prints values so you can see what your code is doing.
String
Text in quotes. + joins strings; length counts their characters.

Practise it in Code Academy

It has 4 exercises (predict the output, fix the broken code and write code from a prompt) and takes about 5 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.