← Explore

Syntax

Syntax means the exact rules for writing a language. Each language has different syntax. Most languages use the same 5 core ideas: variables, conditionals, loops, functions, and lists. This page shows these 5 ideas in Python and in JavaScript.

Variables

A variable is a name for a value. Python and JavaScript detect the value's type automatically. In JavaScript, "let" means the value can change later. "const" means the value cannot change.

Python

age = 16
name = "Ada"
is_student = True

JavaScript

let age = 16;
const name = "Ada";
let isStudent = true;

Conditionals

Run one block of code if a condition is true. Run a different block if the condition is false.

Python

if age >= 16:
    print("Old enough")
else:
    print("Not yet")

JavaScript

if (age >= 16) {
  console.log("Old enough");
} else {
  console.log("Not yet");
}

Loops

Repeat one action for every item in a list. You write the action one time.

Python

for n in [2, 3, 5, 7]:
    print(n * n)

JavaScript

for (const n of [2, 3, 5, 7]) {
  console.log(n * n);
}

Functions

A function is a named block of instructions. You can run a function many times. A function can take input values. A function can return an output value.

Python

def square(n):
    return n * n

square(5)  # 25

JavaScript

function square(n) {
  return n * n;
}

square(5); // 25

Lists / arrays

A list stores many values in order. You can loop over a list. You can add values to a list. You can access one value by its position number.

Python

scores = [88, 92, 79]
scores.append(100)
scores[0]  # 88

JavaScript

let scores = [88, 92, 79];
scores.push(100);
scores[0]; // 88