Values and console.log
console.log(2 + 3) prints 5, but console.log("2 + 3") prints 2 + 3: the quotes decide whether JavaScript calculates or copies.
Everything a program works with is a value
A program pushes data around: prices, names, scores, answers to yes-or-no questions. Each single piece of data is a value. The number 42 is a value, the text "Seoul" is a value, and so is the answer true. Every value has a type, which tells JavaScript what kind of thing it is and what you can do with it. You can multiply two numbers, but multiplying two names makes no sense, so JavaScript needs to know which is which.
The basic types
| Type | Examples | Used for |
|---|---|---|
| number | 42, 3.5, -7 | Counting, measuring, money |
| string | "cat", 'Hello', "42" | Text of any kind |
| boolean | true, false | Yes or no answers |
| undefined | undefined | A value that has not been set yet |
| null | null | Deliberately empty |
JavaScript has a single number type for whole numbers and decimals alike, so 7 and 7.5 are both numbers. A string is a sequence of characters written between quotes; single quotes and double quotes both work as long as the two ends match. A boolean has only two possible values, true and false, written without quotes. The last two, undefined and null, both mean "nothing here," and you will meet them again when a variable has not been given a value. The ECMAScript standard also defines BigInt and Symbol, which this course does not need.
Quotes change the type
The value 42 and the value "42" look the same when printed, but they are different types. The first is a number you can do arithmetic with. The second is a string made of the characters 4 and 2. The operator typeof tells you the type of any value, and it is a quick way to check what you are holding:
console.log(typeof 42); // number
console.log(typeof "42"); // string
console.log(typeof true); // boolean
console.log(typeof "true"); // stringThe same thing happens with calculations. Without quotes, JavaScript works out the expression first and prints the result. With quotes, the characters are text and are printed exactly as typed:
console.log(2 + 3); // 5
console.log("2 + 3"); // 2 + 3A piece of code that produces a value, such as 2 + 3 or typeof 42, is called an expression. Whenever JavaScript meets an expression it evaluates it, which means it reduces it to a single value, and then uses that value. In console.log(2 + 3) the expression is evaluated to 5 before console.log ever sees it.
Printing several values at once
console.log accepts more than one value. Separate them with commas and they are printed on one line with a single space between each:
console.log("Age:", 30); // Age: 30
console.log("Sum:", 4 + 5, "units"); // Sum: 9 unitsThis is the easiest way to label your output while you work. Printing values with a label, like "Sum:" above, is how programmers check what a program is doing at each step. When something behaves strangely, the usual first move is to add a console.log and look.
Worked examples
Example 1. console.log(10 - 4) prints 6, because the expression is evaluated first. Example 2. console.log("10 - 4") prints 10 - 4, because it is a string. Example 3. console.log(typeof "") prints string: two quotes with nothing between them still make a string, called the empty string. Example 4. console.log(typeof 3.0) prints number; there is no separate type for decimals.
| value | One piece of data: 42, "cat", true |
| type | The kind of value: number, string, boolean and so on |
| number | Whole numbers and decimals, one type for both |
| string | Text written between quotes |
| boolean | true or false, written without quotes |
| expression | Code that produces a value, such as 2 + 3 |
| typeof | Gives the type of a value as a string |
| empty string | A string with no characters, written "" |
- What does this print?
console.log(6 * 7); - What does this print?
console.log("6 * 7");- 42
- 6 * 7
- "6 * 7"
- What does this print?
console.log(typeof 7.5); - What does this print?
console.log(typeof "7.5"); - What does this print?
console.log(typeof false); - What does this print?
console.log("Total:", 3 + 4);- Total: 7
- Total:7
- Total: 3 + 4
- Which of these is a boolean value?
"true"true1
- What does this print?
console.log(typeof ""); - What does this print?
console.log(10 - 4, "10 - 4");- 6 6
- 6 10 - 4
- 10 - 4 10 - 4
- Which one is a valid string?
"hello''hello'hello
- Each piece of data is a value, and each value has a type.
- Basic types: number, string, boolean, undefined, null.
- Quotes make a string; without quotes JavaScript evaluates the expression.
- typeof tells you the type.
- console.log(a, b) prints both values on one line with a space.