Skip to content
Programming contents

Lesson 1.7: From pseudocode to JavaScript

JavaScript is a real language and it's already in your browser. The ideas are the pseudocode ones; the words change.

The idea

Pseudocode is for thinking, but it doesn't run. JavaScript does: it's a real programming language and every browser ships with it. Nothing to install.

Open the console

The console is a corner of the browser where you type JavaScript and it runs straight away.

  1. Open a new tab.
  2. Press F12 (on a Mac, Cmd + Option + J) and pick the Console tab.
  3. Type this and press Enter:
JavaScript
console.log("Hello, world");
Console
Hello, world
  • To write several lines without running them, press Shift + Enter at the end of each one.
  • If undefined shows up after a line, it isn't an error: the console shows what each instruction returns, and console.log() returns nothing.
  • If you paste code, the console may ask you to type "allow pasting" first. It's a protection against scams: only paste code you understand.

Same ideas, other words

PseudocodeJavaScript
Write "Hello"console.log("Hello");
Read namelet name = prompt("Name?");
age ← 30let age = 30;
If … Then … EndIfif (…) { … }
While … Do … EndWhilewhile (…) { … }
Function … EndFunctionfunction name() { … }

The next lessons go through them one by one.

The same program, twice

Pseudocode
Algorithm Greet
    Write "What's your name?"
    Read name
    Write "Hello, ", name
EndAlgorithm
JavaScript
let name = prompt("What's your name?");
console.log("Hello, " + name);
  • prompt() opens a window with the question and returns what you type.
  • + joins pieces of text.
  • ; marks the end of each instruction.
  • There's no Algorithm or EndAlgorithm: the program is what you write.

Try it

Exercise

In the console, write a program that asks for a name and a city and answers, for example, "Ana lives in Seville". It's the exercise from 1.2, for real this time.

Show the solution
JavaScript
let name = prompt("What's your name?");
let city = prompt("Which city do you live in?");
console.log(name + " lives in " + city);

Notice the spaces inside " lives in ": without them you'd get "Analives inSeville".

Summary

  • JavaScript is a real language and it runs in the browser.
  • The console (F12) runs it straight away.
  • console.log() writes; prompt() asks.