Types: strings, numbers, booleans
A canteen till stores three very different things about one order: the customer's name, how many cups of tea they want, and whether they have paid. A name is text. A count is a quantity you can multiply. Paid-or-not is a yes-or-no. JavaScript keeps track of which is which, and a lot of confusing bugs come from not noticing when it changed its mind.
let firstName = "Ayesha";
console.log(firstName);
console.log(typeof firstName);
Ayesha string
That second line is the tool this whole lesson is built on.
The three types you will use every day
A string is text. You write it between quotes — single or double, it makes no difference, as long as you close it with the same one you opened.
A number is any quantity: whole or decimal, positive or negative. Unlike
some languages, JavaScript does not separate whole numbers from decimals. 7
and 7.5 are both just numbers.
A boolean is one of exactly two values, true or false. No quotes —
true is a boolean, "true" is a string that happens to spell it.
let studentName = "Fatima";
let cupsOrdered = 3;
let hasPaid = true;
console.log(studentName);
console.log(cupsOrdered);
console.log(hasPaid);
Fatima 3 true
typeof tells you what you actually have
typeof is an operator that answers one question: what kind of value is this?
It hands back a string naming the type.
console.log(typeof "Karachi");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
string number boolean undefined object
Four of those are exactly what you would expect. The fifth one, typeof null
giving object, is a genuine mistake baked into JavaScript in 1995 and never
fixed, because fixing it would break too many existing pages. It is not
logic you need to understand. It is a scar. Remember it and move on.
Quotes change everything
The difference between 16 and "16" looks like nothing on screen and is
enormous in practice. Anything a person types into a web page arrives as a
string, including things that look like numbers.
let ageText = "16";
let ageNumber = 16;
console.log(typeof ageText);
console.log(typeof ageNumber);
console.log(ageText + 1);
console.log(ageNumber + 1);
string number 161 17
The + operator does two different jobs. Between two numbers it adds. With a
string on either side it glues, and it quietly turns the other value into a
string first. So "16" + 1 produces "161", which is almost never what
anyone wanted. Converting text to numbers properly is covered in
the numbers lesson.
Dynamic typing: the label is on the value, not the box
In some languages you declare that a variable holds numbers and it holds numbers forever. JavaScript is dynamically typed: the variable is just a name, and the type belongs to whatever value is sitting in it right now.
let score = 7;
console.log(typeof score);
score = "seven";
console.log(typeof score);
score = true;
console.log(typeof score);
number string boolean
Nothing complained. That flexibility is convenient when you are learning and dangerous when a program grows, because a variable you thought held a number can hold a string by the time it reaches your calculation. Nothing warns you. The value simply behaves differently.
Booleans usually arrive from a question
You rarely type true by hand. Booleans mostly show up as the answer to a
comparison.
let temperature = 41;
let tooHot = temperature > 38;
console.log(tooHot);
console.log(typeof tooHot);
true boolean
temperature > 38 is not a statement of fact. It is a question JavaScript
answers, and the answer is a boolean you can store, print, or act on later.
The lesson on if and else puts these to work.
null and undefined are not the same thing
This is the one that catches everybody, because in casual English both mean "nothing there". In JavaScript they mean two different kinds of nothing, and the difference tells you who is responsible.
undefined means nobody ever put a value here. JavaScript produces it on
its own. null means a person deliberately marked this as empty. It only
appears because you wrote it.
let missingPhone;
let clearedAddress = null;
console.log(missingPhone);
console.log(clearedAddress);
console.log(typeof missingPhone);
console.log(typeof clearedAddress);
undefined null undefined object
Read that as a story. missingPhone was declared and then forgotten — maybe
the form field was never filled in, maybe you spelled the variable name wrong
somewhere. clearedAddress was set to null by a decision: this student has
no address on file, and we know that, and we recorded it.
The practical payoff is diagnostic. When you print a variable and see
undefined, suspect yourself: a typo, a missing assignment, a value that
never arrived. When you see null, the code did what it was told and the
emptiness is real data.
That leads to one habit worth adopting today: never assign undefined
yourself. When you mean "empty on purpose", write null. Keep that rule and
undefined in your data always means exactly one thing — a value that never
got set — which makes it a genuine clue rather than noise.
They are loosely related, which is why the confusion survives:
console.log(null == undefined);
console.log(null === undefined);
true false
Loose == treats them as interchangeable. Strict === does not, because they
are not the same value. Which of those two you should be using is the whole
subject of the equality lesson, and the
short version is: use ===.
Where undefined actually bites
The reason undefined matters is that it poisons arithmetic silently.
let bill;
bill = bill + 250;
console.log(bill);
NaN
No error. No red text. Just NaN — Not a Number — spreading through every
calculation downstream until something prints nonsense on a receipt. If you
had written let bill = 0; the total would be 250. That single missing
starting value is one of the most common beginner bugs there is, and it
never announces itself.
The types you have not met yet
There are more than three. object covers anything with named parts, arrays
are a kind of object, and functions have their own typeof answer. There is
also bigint for whole numbers too large for the normal number type, and
symbol, which you can safely ignore for a long time. Strings, numbers and
booleans will carry you through everything for now.
Print the type when something looks wrong
The habit worth forming today: when a value misbehaves, do not stare at it.
Print it next to its type. console.log accepts several values separated by
commas and prints them on one line.
let enteredAge = "16";
console.log(enteredAge, typeof enteredAge);
16 string
On its own, 16 on the screen tells you nothing — a string and a number look
identical once printed. Add typeof and the answer to "why is my total
wrong?" usually appears in the same second. Do it before you start guessing,
and revisit the variables lesson if you want to
review how values get named in the first place.
Prefer learning with a teacher?
WasiLearn Academy runs small-group classes covering this material, online and in Karachi.
See the classes