Representation Versus Value

A value is the thing itself: the number forty-two. A representation is how that thing is written down: the text "42", the digits 42, the binary 101010.

Every piece of data arriving from outside your program (a form, a file, a web request) arrives as text. Converting text into real values is one of the most common jobs in all of programming.

Text to number

PYTHON

age = int("25")
price = float("19.99")

JAVASCRIPT

const age = Number("25");
const price = parseFloat("19.99");
Compare → Python is explicit about which kind of number you want: int for whole numbers, float for decimals. JavaScript has one number type, so Number handles both, though parseInt and parseFloat exist when you want to be specific.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Store two prices as text, "4" and "6", and print both the joined text and the real total.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Converting in Both Directions →
← Back