Data types

Store someone's name, their age, and whether they are logged in, and you have already used three different kinds of value. The name is text, the age is a number, and the logged-in flag is a yes-or-no. JavaScript treats each of these as a distinct type, and it handles each one differently. This chapter is about the handful of types you meet first, and how to tell which one you are holding.
The basic types
Three types cover most of what you store. A string is text, wrapped in quotes. A number is any number, for counting or measuring. A boolean is a yes-or-no value, and it has only two options: true or false.
const name = "Priya"; // a string: text
const age = 27; // a number
const isLoggedIn = true; // a boolean: true or falseYou use a string for anything that reads like words: a name, a message, a colour. You use a number for anything you might count or calculate with. You use a boolean whenever the answer is one of two options, like whether a box is checked. You will see much more of these in strings and numbers.
string for text in quotes, number for anything numeric, and boolean for a true or false answer. Pick the type that matches the kind of thing you are storing. A name is a string, an age is a number, a checkbox is a boolean. Nothing: null and undefined
Sometimes a value is missing, and JavaScript has two ways to say so. undefined means no value has been given yet. If you make a variable without setting it, its value is undefined.
let score;
console.log(score); // undefinednull means empty on purpose. You use it yourself, to say "this has no value right now, and I meant that".
let winner = null;
console.log(winner); // nullThe short version: undefined is JavaScript saying "nothing here yet", and null is you saying "nothing here, deliberately".
undefined is JavaScript's default when nothing has been set yet. null is the one you write yourself to mean "empty on purpose". If you never gave a variable a value, expect undefined. Checking a type with typeof
When you are not sure what type a value is, typeof tells you. Put it in front of a value and it hands back a string naming the type.
console.log(typeof "Priya"); // "string"
console.log(typeof 27); // "number"
console.log(typeof true); // "boolean"Notice the answer is always text, a string like "number", not the type itself. That is handy when you want to check what you are dealing with before you use it.
typeof in front of a value and it tells you the type, as a string like "number". It is the fastest way to check what you are holding when you are unsure. The answer is always text, so typeof 27 gives you "number", quotes and all. The bigger picture of types
You have met the everyday types: string, number, boolean, plus null and undefined. That is enough to store most of what a small program needs. Later chapters add ways to group values together, but each of those groups is still built from the simple types you have already seen.
For now, the win is knowing that every value has a type, and that the type decides what you can do with the value. A number you can add up; a string you can join together; a boolean you can check. Keep operators in mind as the next step, because that is where you start combining these values.
null and undefined, cover most of what a small program stores. Later chapters group values together, but they are all built from these. Where types go from here
Every value you write, every one you read back, carries a type, and getting fluent at naming it is the base for everything that follows. The next chapters take the two you will handle most and go deep: strings for working with text, and numbers for calculation. After that, operators is where these types start meeting each other, comparing, combining, and occasionally converting, and knowing what type you hold is what keeps that from surprising you.

