The Complete Comparison

You have now met every core concept. This module is your permanent reference, the translation table between two languages and the single way of thinking underneath both.

Variables and constants

PYTHON

name = "Ama"
TOTAL = 100      # convention only

JAVASCRIPT

let name = "Ama";
const TOTAL = 100; // enforced
Compare → Python assigns with no keyword and marks constants by convention with capitals. JavaScript declares with let or const, and const is enforced by the language.
Conditionals

PYTHON

if age >= 18:
    print("adult")
elif age >= 13:
    print("teen")
else:
    print("child")

JAVASCRIPT

if (age >= 18) {
  console.log("adult");
} else if (age >= 13) {
  console.log("teen");
} else {
  console.log("child");
}
Compare → Identical logic. Python uses a colon and indentation, JavaScript uses parentheses and braces, and spells out else if where Python contracts to elif.
Loops

PYTHON

for item in items:
    print(item)

for i in range(5):
    print(i)

while count < 3:
    count += 1

JAVASCRIPT

for (const item of items) {
  console.log(item);
}

for (let i = 0; i < 5; i++) {
  console.log(i);
}

while (count < 3) {
  count += 1;
}
Compare → The for-each form is nearly identical. Counting differs most: Python generates a range, JavaScript states start, condition, and step explicitly.
Functions

PYTHON

def add(a, b=0):
    return a + b

result = add(2, 3)

JAVASCRIPT

function add(a, b = 0) {
  return a + b;
}

const result = add(2, 3);
Compare → Same parameters, same defaults, same return. Only def versus function and the colon versus braces differ.
Collections

PYTHON

nums = [1, 2, 3]
person = {"name": "Ama"}
unique = {1, 2, 3}

len(nums)
nums.append(4)
person["name"]

JAVASCRIPT

const nums = [1, 2, 3];
const person = { name: "Ama" };
const unique = new Set([1, 2, 3]);

nums.length;
nums.push(4);
person.name;
Compare → Lists and arrays are direct equivalents. Dictionaries and objects match closely, with Python requiring quoted keys and bracket access where JavaScript prefers dots.
Errors and JSON

PYTHON

try:
    n = int(text)
except ValueError:
    n = 0

import json
json.dumps(data)
json.loads(text)

JAVASCRIPT

try {
  n = Number(text);
} catch (e) {
  n = 0;
}

JSON.stringify(data);
JSON.parse(text);
Compare → Python catches named error types, JavaScript catches everything in one block. JSON works the same in both, with different function names.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: One Problem, Every Concept →
← Back