From Python to JavaScript
You already know Python, so you know what a program is: a list of instructions the computer runs top to bottom. JavaScript is another language for writing those instructions. It was built to run inside web browsers, and today it also runs on servers through a tool called Node.js. Every interactive thing you have seen on a website (menus opening, likes counting up, forms checking your email) is JavaScript.
In this course we ignore the browser completely. We write pure JavaScript programs that print text to the console, the same way your Python programs printed to the terminal. Everything you learn here transfers directly to browser and server code later.
In Python you print with print(...). In JavaScript the same job is done by console.log(...):
console.log("Hello, JavaScript!");
The line ends with a semicolon. JavaScript can often survive without them, but every serious codebase uses them, so we will too.
Printing a string and a number
The two lines below show the two things console.log is asked to do most often: print some fixed text, and print the result of a calculation. Each call produces exactly one line of output, just as each print(...) did in Python.
console.log("Hello, JavaScript!"); console.log(2 + 3);
Output
Hello, JavaScript!
5The first line printed the characters between the quotes, untouched. The second line printed 5, not 2 + 3, because the arithmetic happens first and console.log only ever receives the finished value.
Comments
A comment is a note for humans that the language agrees to ignore. In Python a comment starts with #. JavaScript uses two slashes for a single line, and /* ... */ to wrap several lines at once:
// this line is ignored /* so is all of this */ console.log("only this runs");
Two other pieces of vocabulary carry straight over from Python. Text inside quotes is a string. Numbers are written bare, with no quotes at all: 5, 3.14. Unit 2 covers the full type system, so for now it is enough to recognize the difference between "5" and 5 when you read a program.
Mixing fixed text with a computed value
This program prints two lines: the fixed text My name is Ada, then the value of 6 * 7. Writing the multiplication instead of the number 42 is deliberate, because it shows that the second argument is an expression the language evaluates rather than text it copies.
console.log("My name is Ada"); console.log(6 * 7);
Output
My name is Ada
42Two habits are worth taking from this small example. Each output line comes from its own console.log(...) call, so the number of calls is the number of lines. And strings are wrapped in quotes while arithmetic is not: console.log("My name is Ada"); prints characters, console.log(6 * 7); prints a computed number.
The JavaScript spelling of print
Python's print("hi") becomes console.log("hi"); in JavaScript. There is no bare print function available to console programs, so a translated Python script has to change that one word everywhere it appears.
The shape of the name matters as much as the spelling. console is an object supplied by the runtime, and log is a function attached to it, which is why the object comes first and the dot follows: console, then .log. Objects are covered properly in unit 5, and until then you can read console.log as one long name for "print a line".