Primitive types
The boxes from the previous lesson hold different items, and each is handled in its own way. A book is read, and an apple is eaten. Values in a program are the same: two numbers can be added, and two words can be joined end to end, as "sun" and "flower" give "sunflower".
The same + sign works differently depending on what stands on its sides:
console.log(2 + 2); // 4
console.log("2" + "2"); // 22Let's go through both lines:
2 + 2— two numbers, and+adds them: the result is4;"2" + "2"— two strings, and+glues them into one: the result is22.
The values 2 and "2" look alike, yet they are of different kinds, and the kind decides what + does with them.
Seven primitive types
The kind of a value is called its type. JavaScript has seven primitive types — the types of simple values that are not made of other values.
number— numbers, whole and fractional:42,-7,3.14.string— strings, text in quotes:"Hi","2".boolean— logical values, of which there are only two:trueandfalse.undefinedandnull— two values that mean "no value".bigintandsymbol— very large whole numbers and special unique values; early on they hardly ever come up.
A string can also be written in single quotes, '2', but this course writes strings in double quotes.
A type belongs to the value
A type belongs to a value, not to a variable. A variable declared with let can hold a number first and, after an assignment, a string.
Back to the storeroom. A let box is not made for one kind of item: today it holds a number, tomorrow a string. The type is told by what lies inside, not by the box itself.
The typeof operator
A sign or word that computes a new value from the values next to it is called an operator. Both + and typeof are operators. The typeof operator returns the name of a value's type as a string.
console.log takes several values separated by commas and prints them side by side, separated by spaces:
const name = "Ann";
console.log(typeof 42, typeof name); // number string
console.log(typeof true, typeof undefined); // boolean undefined
console.log(typeof 3.14, typeof NaN); // number numberLet's go through the output line by line:
typeof 42givesnumber. Fortypeof name, JavaScript looks at the value in the variable,"Ann", and givesstring.trueis aboolean, and the type ofundefinedhas the same name as the value itself.- Both
3.14andNaNgivenumber.
The last line shows two quirks:
- fractional numbers have no type of their own:
3.14is anumbertoo; NaN(not a number) is a special value of typenumberthat means "not a number".
The typeof null quirk
The typeof operator gives an unexpected answer for null:
console.log(typeof null); // objectThis is an old mistake in the language: null is a primitive value, but typeof calls it "object". That is the name of the type of compound values, and null is not one of them. The mistake cannot be fixed without breaking programs already written, so the answer "object" for null has to be memorized.
Interviewers often ask what typeof null returns. The right answer is "object", and it is worth adding that this is an old mistake in the language, not the real type of null.
Common mistake
A string was written without quotes:
const greeting = hello; // ✗
console.log(greeting);ReferenceError: hello is not definedWithout quotes, hello is a variable name. JavaScript looks for a variable with that name, does not find it and stops the program on this line. Text becomes a value of type string only in quotes: const greeting = "hello";.
Key points
- A type is the kind of a value; the type decides what an operator such as
+does with the value. - There are seven primitive types:
number,string,boolean,undefined,null,bigintandsymbol. - A type belongs to a value, not to a variable: a
letcan hold a number first and a string later. typeofreturns the type's name as a string:typeof NaNis"number", andtypeof nullis"object".
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.