CodeStride
Getting Started

Your first program in JavaScript

Topic 1 of 64 · 6 minFree lesson

JavaScript is the language that makes websites react. Every time a page opens a menu, checks a form or loads new posts as you scroll, JavaScript is doing the work. It runs in every web browser, so it's one of the most useful languages you can learn.

A program is a list of instructions for the computer. You'll start with programs that print messages. Later in the course, you'll use the same skills to make web pages come alive.

Your first line of JavaScript

console.log() prints whatever you put between its brackets. The console is the place where a program shows its messages. Here, it's the output box under your code.

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

Text in quotes is called a string. The quotes tell JavaScript where the text starts and ends. They don't show up in the output.

One line after another

A program runs from top to bottom, one line at a time. Each console.log() prints on a new line.

Example 2
console.log("Good morning!");console.log("Today is a great day to code.");console.log("Let's begin.");
Good morning!Today is a great day to code.Let's begin.

Each line ends with a semicolon ;. It marks the end of one instruction, like a full stop at the end of a sentence.

Numbers and math

Numbers don't need quotes. JavaScript works out the math and prints the answer.

Example 3
console.log(2 + 3);console.log(10 * 4);
540

Put the same sum in quotes, and it becomes text. JavaScript prints it exactly as you wrote it.

Example 4
console.log("2 + 3");
2 + 3

Several values at once

You can give console.log() several values, with commas between them. It prints them on one line, with a space in between.

Example 5
console.log("Apples:", 6);console.log("Total:", 6 + 4);
Apples: 6Total: 10
Common mistakeForgetting the quotes around text. Without them, JavaScript thinks the word is the name of something you made earlier. It can't find it, so it stops with a ReferenceError.
Example 6
console.log(Hello);
ReferenceError: Hello is not defined
TipCapital letters matter in JavaScript. console.log works, but Console.log or console.Log stops with an error. When something doesn't work, check the capitals first.

Try it yourself

Run the code in the editor and answer 8 practice questions on your first program. It's free, you only need an account.