Course outline · 0% complete

0/29 lessons0%

Course overview →

Your first JavaScript program

lesson 1-1 · ~8 min · 1/29

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.

JavaScriptyour .js codeBrowserbuttons, menus, pagesNode.jsservers, tools, this course
The same JavaScript language runs in two places: inside web browsers and in Node.js. This course uses console programs, so your code behaves like your Python scripts did.

Code exercise · javascript

Run this program. It prints a string, then the result of a calculation. Notice that console.log works like Python's print: one call per output line.

Comments

In Python a comment starts with #. JavaScript uses two slashes for a single line and /* ... */ for several lines:

// this line is ignored
/* so is
   all of this */
console.log("only this runs");

Text inside quotes is a string, exactly as in Python. Numbers are written bare: 5, 3.14. You will meet the full type system in unit 2. For now the loop you should build is: write a line, run it, read the output.

Code exercise · javascript

Your turn. Make the program print exactly two lines: first the text My name is Ada and then the result of 6 * 7 (use math, do not type 42).

Quiz

In Python you wrote print("hi"). What is the JavaScript equivalent?