Data types
🏠The everyday picture
A form asks for your name (words), your age (a number) and "Do you agree? Yes/No" (a choice of two). Each kind of answer is handled differently: you can add ages, but not names.
📘What it really is
- The basic types of data:
- string: text in quotes:
"Patna",'hello', or with backticks for templates (below); - number:
42,3.5,-7. JavaScript has one number type for whole numbers and decimals; - boolean:
trueorfalse; - undefined: a variable that has no value yet;
-
null: deliberately "no value".
-
Two ways to group data (more in Level 3):
- array: an ordered list, counted from 0:
const days = ["Mon", "Tue", "Wed"];sodays[0]is"Mon"; - object: named pieces of information about one thing:
const learner = { name: "Asha", city: "Patna", level: 2 };
console.log(learner.city);
-
Template strings use backticks and
${}to put values inside text:`Hello, ${learner.name}`. -
typeoftells you a value's type:typeof 42is"number". - A classic trap: anything typed into a form arrives as a string.
"5"is text, not the number 5.
🖱️Try it
In the console, make an object describing yourself (name, city, modules done, and whether you've built a site:
true or false). Print a sentence about yourself using a template string. Then try typeof on each value.
✅Quick check
What's days[1] if days is ["Mon", "Tue", "Wed"]?
Show the answer
"Tue", because arrays count from 0.
A form's age field gives "25". What type is it?
Show the answer
A string. It must be converted to a number before doing arithmetic.
