Values, variables, and the type that trips everyone
By the end of this lesson you will have play.js, a file you typed yourself, printing results you predicted before running it — with at least one prediction wrong, and now explained.
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. You will also meet var, the older form of let, in code an assistant generates; read it as let.
Everything after // on a line is a comment: a note for the reader that the computer ignores. The examples here use them to show what a line produces.
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 Backtest Strategies 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 and put these two lines in it, wrapped in console.log( ) so the file prints them:
console.log("5" + 3);
console.log("5" - 3);
Run node play.js. Then add two more lines, console.log("10" * 2); and console.log("10" + 2);. Before you save and run the file again, write down what you expect each to print. Getting one wrong is the point of the exercise.
The prompt
Write and run play.js yourself first. Then paste it into your chat assistant:
I am learning JavaScript and typed this file by hand. Do not rewrite
it. For each console.log line, tell me what it prints and why, in one
sentence. Then give me one more line in the same style whose result
would surprise a beginner, without telling me the answer.
[paste play.js]
A good answer explains + joining text and - converting it, line by line, and hands you a new puzzle rather than its solution. Run the puzzle yourself before asking for the answer.
Check it yourself
node play.jsprints53and then2. If it prints nothing at all, wrap each line inconsole.log( ): a file prints only what you ask it to."10" * 2prints20and"10" + 2prints102. Compare both with what you predicted.- Add
console.log(typeof ("10" + 2)). It printsstring, although the line above it printed something that looked like a number. - You can say in one sentence why
-and*turn the text into a number while+does not.
Break it on purpose
Add console.log(prise), misspelling price (and declare const price = 309.90; above it if the file has none), and run the file. Read the error: ReferenceError: prise is not defined, with your file name and a line number. Unit 4 turns reading exactly this into a method. Delete the misspelled line and run the file again before you commit, so the version in your history runs.
Compare with ours
Our play.js
const price = 309.90;
console.log("5" + 3); // 53
console.log("5" - 3); // 2
console.log("10" * 2); // 20
console.log("10" + 2); // 102
console.log(typeof price); // number
console.log(typeof ("10" + 2)); // string
Node prints 53 and 102 without quotes, which makes them look like numbers. typeof is how you find out they are not.
Checkpoint
git status # only the files you expect, and no .env*
git add -A
git commit -m "play.js: + joins text, - and * convert it, typeof tells them apart"
Then answer this lesson's questions.