JavaScript Data Types and typeof
Strings, numbers, booleans, null, undefined, arrays and objects, and how typeof tells you what you actually have. After this, "51" instead of 6 stops being a mystery.
Course content
Sign in to save your progress.
-
Before You Write Code
-
What JavaScript Actually Does in Your Browser
-
Running JavaScript in the Console and in a Script File
-
-
-
The Basics You Cannot Skip
-
JavaScript let, const, and Why Not var
-
JavaScript Data Types and typeof
-
JavaScript == vs ===: Comparing Values Without Surprises
-
-
-
Making Decisions and Repeating Work
-
JavaScript if, else and the Ternary Operator
-
JavaScript Loops: for, for...of and while
-
-
-
Organizing Your Code
-
JavaScript Functions and Arrow Functions
-
JavaScript Arrays and Objects
-
-
-
Touching the Page
-
Changing the Page With the JavaScript DOM
-
JavaScript Click and Form Events
-
Reading JavaScript Errors in the Browser Console
-
-
- Final Check: JavaScript for Beginners
Skip ahead: If you know why "5" + 1 gives "51" and what typeof null returns, skip to "Common mistakes".
Every value in JavaScript has a type, and the type decides what operations do. 5 + 1 is 6. "5" + 1 is "51". Same symbols, different result, because one of them is text. Most beginner bugs with numbers come from this, specially with values that come from a form.
The types you'll use every day
- string: text, in quotes.
"Ana",'Manila', `Hello`. - number: whole numbers and decimals are the same type.
42,3.5,-10. - boolean:
trueorfalse. - undefined: a variable exists but has no value yet.
- null: "no value", set on purpose.
- object: a group of labelled values,
{ name: "Ana", age: 28 }. Arrays,["rice", "eggs"], are objects too.
There are a few more (bigint, symbol). You won't need them in this course.
typeof tells you what you have
When something behaves strangely, check the type before you check anything else.
console.log(typeof "250");
console.log(typeof 250);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof { name: "Ana" });
console.log(typeof ["rice", "eggs"]);
console.log(typeof null);
string
number
boolean
undefined
object
object
object
Two of those surprise people.
- An array says
object. To check for an array, useArray.isArray(value). nullsaysobject. That's a bug from the first version of JavaScript that can't be fixed now without breaking old websites. To check for null, compare directly:value === null.
Why "5" + 1 is "51"
+ does two jobs. With two numbers it adds. If either side is a string, it joins text. Other math operators only work on numbers, so they convert the string first.
console.log("5" + 1);
console.log("5" - 1);
console.log("5" * 2);
console.log(5 + 1);
51
4
10
6
Now the real-world case. Anything you read from an input field is a string, even if the visitor typed a number. So this goes wrong:
const quantityFromInput = "3";
const price = 250;
console.log(quantityFromInput + price);
console.log(Number(quantityFromInput) * price);
3250
750
The first line didn't add anything. It glued "3" and "250" together. The fix is to convert on purpose with Number() before you do math.
⚠ Warning: When the conversion fails, you don't get an error. You get NaN ("not a number"), and every calculation with NaN gives NaN. Number("abc") is NaN. A total that shows NaN on the page usually means one input wasn't a valid number. Check with Number.isNaN(value) right after converting, not five steps later.
const typed = "12 pcs";
const qty = Number(typed);
if (Number.isNaN(qty)) {
console.log("Please enter a number, got:", typed);
}
Please enter a number, got: 12 pcs
undefined vs null
Both mean "nothing", but they say different things.
undefinedusually means "nobody set this yet". Aletwith no value, a missing object property, a function that doesn't return anything.nullmeans "I checked, and there's nothing". You set it yourself, for examplelet selectedProduct = null;before the visitor picks one.
In your own code, use null when you mean "empty on purpose". Leave undefined to JavaScript. Then when you see undefined somewhere unexpected, you know something wasn't set, not that you chose it.
Truthy and falsy
In an if, every value counts as true or false. These are falsy: false, 0, "" (empty string), null, undefined, NaN. Everything else is truthy, including "0", "false" and an empty array [].
That's convenient (if (name) checks for a non-empty name) and dangerous (if (quantity) treats 0 as "missing"). When 0 is a valid value, compare explicitly.
💡 Tip: When a result looks wrong, log the value and its type together: console.log(value, typeof value). It takes five seconds and answers half your questions.
Common mistakes
- Doing math on input values without
Number(). - Checking
typeof x === "array". It's never true. UseArray.isArray(x). - Treating
0or""as missing because they are falsy. - Writing
"true"(a string) instead oftrue(a boolean)."false"is truthy.
What happens if you skip this
Your totals will be right in testing, because you typed numbers in the code, and wrong on the page, because the browser gives you strings. Customers see ₱3250 instead of ₱750, and the code looks correct when you read it.
Quick check: what does "10" + 5 - 5 give, and why?
About
Strings, numbers, booleans, null, undefined, arrays and objects, and how typeof tells you what you actually have. After this, "51" instead of 6 stops being a mystery.
- Duration
- 5 min
- Last update
- 08/23/2026
Comments (0)
Sign in to commentNo comments yet.