A stall's online checkout adds a price of $15 to a delivery fee of $2. You expect $17. But if the price is stored as text ("15", in quote marks), JavaScript joins the two values into "152". Both cases use the same +. The only difference is the kind of value: a number or text.
The kind of a value is called its data type. Examples: text, a number, a yes or no, a list. The data type (often just called the type) decides what you can do with a value: you can add numbers, and you can join text.
Three basic types: string, number, boolean
This code makes three variables with different types, then prints each type:
let city = "Leeds";
let price = 15;
let hungry = true;
console.log(typeof city); // "string"
console.log(typeof price); // "number"
console.log(typeof hungry); // "boolean"
"Leeds"is a string, which means text. A string always goes inside quote marks,"..."or'...'.15is a number. It has no quote marks, it can have decimals with a dot (3.14), and it can be negative (-2).trueis a boolean: a type with only two values,trueorfalse, without quote marks. You use a boolean for a yes or no answer.typeofis an operator (a word or sign that works on values, like+).typeofbefore a value, or before a variable as intypeof city, gives the name of its type as text:"string","number"or"boolean". The quote marks in the comment only show that the result is text: the console printsstringwithout them.city,priceandhungryare just the variables' names, and other names would work too. You don't write the type in the code. JavaScript knows the type from the value.
Quote marks change the type. 42 is a number, "42" is a string. So + gives different results: 42 + 1 gives 43, while "42" + 1 gives "421". When one of the values is a string, + joins instead of adding. In the same way, "true" in quote marks is a string, not a boolean.
There are two more basic types that beginners rarely use. bigint is for very large whole numbers, written with n at the end (10n). symbol is for names that are sure to be unique (never the same as any other name).
Arrays and objects: groups of values
The next two types hold several values together:
let shopping = ["rice", 2, true];
let position = { x: 3, y: 7 };
shoppingholds an array: an ordered list of values. An array is written inside square brackets[ ], with a comma between the values. It can mix types: this one holds a string, a number and a boolean.positionholds an object: a group of name and value pairs. An object is written inside curly braces{ }, with a comma between the pairs. One pair is called a property. In the first pair,xis the property's name and3is its value. Inside an object,:separates the name from the value, not=.
Before you read on, guess: what does typeof [1, 2, 3] give?
The answer is "object", not "array". In JavaScript, an array is a kind of object. To be sure a value is an array, use Array.isArray(shopping). It gives true.
Empty: undefined and null
Two values mean empty. This code uses both:
let address;
let photo = null;
console.log(address); // undefined
console.log(typeof photo); // "object"
console.log(photo === null); // true
undefinedmeans no value has been given yet. The variableaddresswas made without=, so it holdsundefined.nullmeans emptied on purpose. You writenullas the value yourself, as on the variablephoto.typeof nullgives"object", althoughnullis not an object. This is a bug (a mistake) from the first version of JavaScript, left in on purpose so that old websites don't break. To check fornull, compare directly:photo === null. The===sign checks whether two values are exactly the same, including their type.
⚠️ Misplaced quotes and a misleading typeof
- Writing the type as the variable's name.
'string' = "Learning JS"is an error. The left side of=must be a variable name, for examplelet label = "Learning JS";. Its type is a string automatically, because the value is text. - Putting quotes around a number or a boolean.
"42"and"true"are strings. Write42andtruewithout quote marks. - Checking for an array with
typeof. It gives"object", the same as a plain object andnull. UseArray.isArray(). - Decimals that are not exact.
0.1 + 0.2gives0.30000000000000004, because the computer stores decimals in binary (only 0s and 1s). For money, store prices as whole numbers of cents (1500for $15), and change how they look only when you show them. For example,(1500 / 100).toFixed(2)writes the cents as dollars with two decimals, and gives"15.00".
TL;DR: A data type is the kind of a value. A string is text inside quote marks, a number has no quote marks, a boolean is true or false. An array is a list of values inside [ ], an object is a group of name and value pairs inside { }. undefined means no value has been given yet, null means emptied on purpose. typeof before a value gives the name of its type as text. But for an array and for null, typeof gives "object" both times. So check for an array with Array.isArray(), and check for null with === null (=== means exactly the same).
⚡ String, number, boolean
Write:
let city = "Leeds";
let price = 15;
let hungry = true;
console.log(typeof price); // "number"
Meaning: Every value has a data type, which is its kind. A string is text inside quote marks, a number has no quote marks, and a boolean is only true or false. typeof price gives the type of what price holds, as text: here "number".
⚡ Quote marks change the type
Guess first: what does "42" + 1 give?
"421". "42" in quote marks is a string, and + joins strings instead of adding. Without quote marks, 42 + 1 gives 43.
Meaning: The type comes from the value, not from the variable's name. A value in quote marks is always a string: "true" is a string, not a boolean.
⚡ An array says it's an object
Write:
let shopping = ["rice", 2, true];
console.log(Array.isArray(shopping)); // true
Guess first: what does typeof [1, 2, 3] give?
"object", not "array". An object is the type for a group of values, and in JavaScript an array is a kind of object.
Meaning: An array is a list of values inside [ ]. To be sure a value is an array, use Array.isArray(). typeof null also gives "object" (null is a value that means emptied on purpose). So check for null directly: photo === null for a variable named photo. The === sign checks whether two values are exactly the same.