‹ Start From Zero Lesson 5 of 21
Contents Lesson 5 of 21

3 min read · foundations

Values, variables, and the type that trips everyone

A variable is a name for a value. That is the whole idea, and the interesting part is what happens when the values are of different kinds.

Naming a value

const price = 309.90;
const ticker = "AAPL.US";
let count = 0;

const means the name will not be pointed at something else; let means it might. Prefer const — a name that cannot change is one less thing to track while reading.

The four kinds you will meet constantly

Number — 309.90. No separate integer type; there is one number kind.

String — "AAPL.US", text in quotes.

Boolean — true or false.

Null and undefined — "deliberately nothing" and "never given a value". The distinction matters more than it looks: in market data, a missing figure and a zero are different facts, and the whole track keeps returning to it.

The one that trips everyone

"5" + 3      // "53"   — string, because + on a string joins
"5" - 3      //  2     — number, because - has no string meaning

A number that arrived as text behaves like text until something forces it not to. This is not a curiosity: API responses deliver numbers as strings all the time, and course 3 meets a real one — a stock split arriving as "4.000000/1.000000", a ratio you cannot multiply by until you have parsed it.

When a number does something strange, check its type first:

console.log(typeof price);   // "number" or "string"

console.log is your instrument

It prints a value so you can see it. Almost all beginner debugging is putting one of these above the line that misbehaves and looking at what is actually there rather than what you assumed.

Try it now

Make a file play.js, put those two lines in it, and run node play.js. Then try "10" * 2 and "10" + 2 and predict each before you press enter. Getting one wrong is the point of the exercise.