First steps: programs, values and variables · Part 2 of 36
Variables and data types
Basic 14 min
How you store values in variables, which basic data types exist, and why 0.1 + 0.2 is not exactly 0.3.
What you will learn
- You can declare variables in JavaScript (let/const) and Python.
- You can name the basic data types: numbers, strings, booleans and "nothing".
- You can explain why floating-point results are sometimes slightly off.
Before this: What is a program?
A variable is a name for a value
A variable is a name that refers to a value, so you can use the value later and change it. Think of a label you stick on a box rather than the box itself.
// JavaScript
let apples = 5; // can be reassigned later
const pricePerApple = 0.4; // cannot be reassigned
apples = apples + 2; // now 7
console.log(apples * pricePerApple);
# Python
apples = 5
PRICE_PER_APPLE = 0.4 # convention: CAPITALS mean "don't change"
apples = apples + 2 # now 7
print(apples * PRICE_PER_APPLE)
In JavaScript you declare a variable with let (may change) or const (may not be reassigned). Prefer const and use let only when the value really changes; it makes code easier to read. The old keyword var still works but has confusing scoping rules — avoid it in new code. Python has no declaration keyword: the first assignment creates the name.
The = sign means assignment, not equality. apples = apples + 2 reads "compute apples + 2, then let apples refer to the result".

Basic data types
Every value has a type that decides what you can do with it.
| Idea | JavaScript | Python |
|---|---|---|
| Whole number | 42 (type number), very large: 42n (bigint) | 42 (int, any size) |
| Decimal number | 3.14 (also number) | 3.14 (float) |
| Text | "hi" or 'hi' (string) | "hi" or 'hi' (str) |
| Truth value | true, false (boolean) | True, False (bool) |
| "Nothing" | null, undefined | None |
You can ask for the type: typeof 42 gives "number" in JavaScript, type(42) gives <class 'int'> in Python.
Both languages are dynamically typed: a variable can hold a number now and a string later. That is convenient, but it lets bugs slip through, which is why many teams add type annotations (TypeScript for JavaScript, type hints checked by tools such as mypy for Python).
Mixing types
The two languages differ when you mix types. JavaScript tries to convert for you: "5" + 1 is the string "51", while "5" * 2 is the number 10. Python refuses: "5" + 1 raises a TypeError. Convert explicitly instead: Number("5") or parseInt("5", 10) in JavaScript, int("5") or float("5.5") in Python.
To compare values in JavaScript, always use === (strict equality, no conversion) instead of ==. In Python == already compares without such conversions.
Why 0.1 + 0.2 is not 0.3
Try 0.1 + 0.2 in either language: you get 0.30000000000000004. This is not a bug in the language. Decimal numbers are stored in binary floating point (IEEE 754 double precision, 64 bits). Just as 1/3 has no exact decimal form, 0.1 has no exact binary form, so a tiny rounding error appears.
Practical rules:
- Never compare floats with
===/==directly; check whether the difference is tiny:Math.abs(a - b) < 1e-9or Python'smath.isclose(a, b). - For money, count in whole cents (integers) or use a decimal type (Python's
decimal.Decimal).
Try it: JavaScript sandbox
Write and run small JavaScript programs; the checker tells you when an exercise is solved.
Turn on JavaScript to use this lab. Worked example:
Exercise 2 gives you let total = 0.1 + 0.2; and asks you to print true when total is close to 0.3. One solution is console.log(Math.abs(total - 0.3) < 1e-9).
Key points
- A variable is a name that refers to a value;
=assigns, it does not compare. - JavaScript declares with
const(preferred) orlet; Python creates a name on first assignment. - Basic types: numbers, strings, booleans and "nothing" (
null/undefinedvsNone). - JavaScript converts types implicitly ("5" + 1 = "51"); Python raises a TypeError. Use
===in JavaScript. - Floating-point numbers are binary approximations: compare with a tolerance and count money in cents.
Flashcards
- Question: What is the difference between let and const in JavaScript?
- Answer: let can be reassigned; const cannot.
- Question: What does "5" + 1 give in JavaScript and in Python?
- Answer: "51" in JavaScript; a TypeError in Python.
- Question: Why is 0.1 + 0.2 not exactly 0.3?
- Answer: Decimals are stored as binary floating point (IEEE 754), and 0.1 has no exact binary form.
- Question: How should you store money amounts?
- Answer: As whole cents (integers) or with a decimal type.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Which JavaScript keyword creates a variable that cannot be reassigned?
- const
- let
- var
- static
Show the answer
const
const creates a binding that cannot be reassigned; let allows reassignment.
Source: MDN JavaScript Guide, Grammar and types
What is the result of "5" + 1 in JavaScript?
- "51"
- 6
- TypeError
- NaN
Show the answer
"51"
With a string operand, + concatenates, so 1 becomes "1" and the result is "51".
Source: MDN JavaScript Guide, Expressions and operators
Match the value with its Python type.
Show the answer
42 = int; 3.14 = float; "hi" = str; True = bool
These are Python’s built-in basic types.
Source: The Python Tutorial, ch. 3
Which are good ways to check whether a float result equals 0.3?
- math.isclose(x, 0.3) in Python
- Math.abs(x - 0.3) < 1e-9 in JavaScript
- x === 0.3 in JavaScript
- x == 0.3 in Python
Show the answer
math.isclose(x, 0.3) in Python; Math.abs(x - 0.3) < 1e-9 in JavaScript
Floating-point values carry tiny rounding errors, so compare with a tolerance.
Source: IEEE 754-2019
Sources
- MDN Web Docs JavaScript Guide, Mozilla, 2025
- The Python Tutorial (3.14), Python Software Foundation, 2025
- IEEE 754-2019 Standard for Floating-Point Arithmetic, IEEE, 2019
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.