{ }codelper

Learn › JavaScript

Basics · 02

Variables and values

Give a value a name so you can reuse and change it.

A variable is a name tag on a value.
The example below stores a name and an age, prints them, then adds 1 to the age and prints it again.

const name = "Lara";let age = 10;console.log(name + " " + age);age = age + 1;console.log(age);
stdout
Lara 10
11

Key points

  • = does not mean "is equal to".
    It means "put the value on the right into the name on the left", which is why age = age + 1 makes sense.
  • Some languages make you write a type (integer, text, …) when you create a variable.
    In others the value knows its own type, so you don't.
  • A value that should never change is a constant.
    Assigning to a constant again is an error.
  • Integers have a size limit.
    A 32-bit int goes up to 2,147,483,647 (about 2.1 billion); for bigger numbers use a 64-bit integer (up to about 9.2×10^18).

How each language writes it

Pythonage = 10 — no type.
There is no constant syntax, only the convention of an uppercase name like MAX_SCORE.
Integers have no size limit.
Swap with a, b = b, a
JavaScriptlet age = 10, constants with const (reassigning one throws TypeError: Assignment to constant variable. at run time).
Numbers are exact only up to 2^53 (about 9×10^15).
Swap with [a, b] = [b, a]; put a value in text with ${age}
TypeScriptlet age: number = 10 — leave the type out and it is inferred from the value.
Reassigning a const is a compile error.
Otherwise the same as JavaScript
Javaint age = 10;, text is String, and var lets the compiler infer.
Constants use final, big numbers long.
Swap with a temporary
C#int age = 10;, text is string, var also works.
Constants use const, big numbers long.
Swap with (a, b) = (b, a); put a value in text with $"{age}"
C++int age = 10;, text is std::string, and auto lets the compiler infer.
Constants use const, big numbers long long.
Swap with std::swap(a, b)
Cint age = 10;, text is const char * (or a char array).
Constants use const, big numbers long long.
Swap with a temporary.
Put text and numbers into slots: printf("%s %d\n", name, age)
Craykonage = 10; — no type, and no constants.
Numbers are exact up to 2^53.
Swap with a temporary.
me is a keyword, so it can't be a variable name

Common mistakes

Swapping with a = b; b = a;

a = 1; b = 2;
a = b;
b = a;

→ Both a and b are 2: a's original value was lost on the first line

Use a temporary: t = a; a = b; b = t;

Adding to a variable with no starting valueC · C++

int total;
total += 5;
printf("%d\n", total);

→ It starts from whatever garbage was in memory, so the sum is wrong

Give it a starting value: int total = 0;.
Java and C# reject such code at compile time.

Using a variable outside the block that created itCraykon

if (true) {
    y = 1;
}
Console.WriteLine(y);

→ An error saying the name y hasn't been made yet

Create it before the block with y = 0;.
(Python, by contrast, still sees it outside.)

How languages differ
Python can't join text and a number with + ("Lara " + 10 → TypeError: can only concatenate str (not "int") to str).
Convert with str(age) or use f"{name} {age}".

Practice