Skip to main content

Chapter 1 - Python Basics (JavaScript)

Here's a JavaScript-flavoured version of the same concepts, with small JS examples for each idea.


Expressions and the REPL

An expression is a combination of values and operators that evaluates to a single value, and you can try them interactively in a REPL (like browser DevTools console or Node.js).

Example: basic math expressions.

2 + 3        // 5
10 - 4 // 6
3 * 7 // 21
22 / 8 // 2.75
Math.floor(22 / 8) // 2 (integer division equivalent)
22 % 8 // 6 (remainder/modulo)
2 ** 10 // 1024 (exponent)

JavaScript has no // integer division operator; use Math.floor() instead.


Math operator precedence

JavaScript follows standard order of operations: exponentiation (**) first, then * / %, then + -. Use parentheses to override.

2 + 3 * 6        // 20  (multiplication first)
(2 + 3) * 6 // 30 (parentheses override)

Data types: numbers, strings, and more

JavaScript has number (covers both integers and floats), string, boolean, undefined, null, and others. There is no separate int vs float type.

42           // number
3.14 // number
"Hello" // string

You can check the type of a value with typeof:

typeof 42        // 'number'
typeof 3.14 // 'number'
typeof "Hello" // 'string'

String concatenation and replication

The + operator joins strings together, just like Python. For replication, JavaScript has no * operator on strings, but you can use .repeat().

Example: combining and repeating strings.

"Alice" + " " + "Bob"   // 'Alice Bob'
"Ha".repeat(3) // 'HaHaHa'

Unlike Python, JavaScript will silently coerce numbers to strings with +, which can surprise you:

"I am " + 29 + " years old"     // "I am 29 years old" (auto-coercion)
"I am " + (29 + 1) + " next year" // "I am 30 next year" (parens force math first)

You can also use template literals for cleaner string building:

const name = "Alice";
console.log(`Hello, ${name}!`); // 'Hello, Alice!'

Variables and assignment statements

In JavaScript, you declare variables with let (can change) or const (cannot reassign). Avoid var in modern code.

Example: storing and updating a value.

let spam = 42;
console.log(spam); // 42

spam = spam + 1;
console.log(spam); // 43

spam = "Hello";
console.log(spam); // Hello (variables can change type with let)

Use const when the value won't change:

const pi = 3.14159;
// pi = 3; // TypeError: Assignment to constant variable

Variable naming rules

Variable names in JavaScript must follow these rules:

  1. Can contain letters, numbers, underscores, and $.
  2. Cannot start with a number.
  3. Cannot be a reserved keyword (like if, for, class).
let myName = "Alice";      // Valid
let _count = 10; // Valid
let item2 = "book"; // Valid
let $price = 9.99; // Valid ($ is allowed in JS)

// let 2things = "nope"; // Invalid: starts with a number
// let class = "nope"; // Invalid: class is a keyword

By convention, variable names use camelCase in JavaScript (not snake_case).


Comments

Single-line comments start with //. Multi-line comments use /* ... */.

// This is a comment
let spam = 1; // This is an inline comment

/*
Multi-line comments are also available
for longer explanations.
*/
const total = price * 1.08;

console.log() (equivalent of print)

console.log() displays values in the console, similar to Python's print().

console.log("Hello, world!");           // Hello, world!
console.log("My age is", 29); // My age is 29
console.log("cats", "dogs", "mice"); // cats dogs mice

Getting user input

In the browser, prompt() pauses and asks the user for input. It always returns a string (or null if cancelled).

Example: ask the user for their name.

const name = prompt("What is your name?");
console.log("Hello, " + name);

Because prompt() always returns a string, you need to convert it if you want a number:

const age = prompt("What is your age?");   // '29' (string)
const ageNum = Number(age); // 29 (number)

In Node.js, you'd use the readline module:

const readline = require("readline");
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });

rl.question("What is your name? ", (answer) => {
console.log("Nice to meet you, " + answer);
rl.close();
});

Your first program (console version)

The equivalent of the book's first complete program, translated to JavaScript:

// This program says hello and asks for my name.
console.log("Hello, world!");
const myName = prompt("What is your name?");
console.log("It is good to meet you, " + myName);
console.log("The length of your name is:");
console.log(myName.length);

const myAge = prompt("What is your age?");
console.log("You will be " + (Number(myAge) + 1) + " in a year.");

String length (equivalent of len)

In JavaScript, strings have a .length property (not a function call).

"hello".length      // 5
"".length // 0
"Hi there".length // 8

Type conversions: String(), Number(), parseInt(), parseFloat()

You can convert values between types using these functions.

String(29)          // '29'
Number("42") // 42
parseFloat("3.14") // 3.14

parseInt(7.8) // 7 (truncates, like Python's int())
Number("hello") // NaN (Not a Number)

A practical example:

const age = prompt("Enter your age:");
const yearsLeft = 100 - Number(age);
console.log(`You have ${yearsLeft} years until 100.`);

Math.round() and Math.abs() (equivalent of round and abs)

Math.round(3.5)          // 4
3.14159.toFixed(2) // '3.14' (returns string)
Math.abs(-42) // 42
Math.abs(42) // 42

Binary and how computers store numbers

Computers represent all data in binary (base-2, only 0s and 1s). JavaScript lets you work in decimal normally, but you can write binary literals with 0b.

0b1010          // 10 in decimal
0b111 // 7 in decimal
(13).toString(2) // "1101" (decimal to binary string)

Overall idea of the chapter

The chapter's main message is: JavaScript programs are built from expressions, data types, variables (let/const), and built-in functions like console.log, prompt, String, Number, and the .length property. The core ideas are the same as Python, but the syntax differs in small but important ways (camelCase, typeof, .repeat(), template literals).