2 / 12 · Intro
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.
Jump to the 10 practice questionsYou will learn
- Check a type with typeof
- Tell an expression that is worked out from text that is copied
Before this: What programming is
1Explain
Values and their types
The number 42 and the text "Seoul" are two values a program can store. 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.
| 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 |
2Explain
typeof names 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"); // stringnumber
string
boolean
string3Explore
Check any value
Pick a value. Quotes always make a string, even "42" and "true"; the second line shows how the value prints.
console.log(typeof 42);
console.log(42);number
424Problem
The type of "7.5"
console.log(typeof "7.5");Hint
Look at the quotes first.
Solution
Answer: string
The quotes make it a string.
5Explain
Calculate or copy
The 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 + 35
2 + 36Problem
A sum in quotes
What does this print?
console.log("6 * 7");Hint
Inside quotes, 6 * 7 is only characters.
Solution
Answer: 6 * 7
It is a string, so the characters are printed as typed, without the quotes.
7Explain
Several values in one line
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 unitsAge: 30
Sum: 9 units8Problem
A number and a string
What does this print?
console.log(10 - 4, "10 - 4");Hint
Work out each value on its own: one is an expression, one is text.
Solution
Answer: 6 10 - 4
The first value is an expression and becomes 6. The second is a string and is printed as typed.
9Practice
Two types, two lines
Print the type of 42 and then the type of "42", one per line.
number
stringconsole.log(typeof 42);
// Add a line that prints the type
// of "42" (with quotes).
Hint
Copy the first line and put quotes around 42 in the copy.
A solution
console.log(typeof 42);
console.log(typeof "42");10Practice
Hours in a year
This prints the calculation as text. Change it so JavaScript calculates the number of hours in 365 days and prints the result.
8760console.log("365 * 24");
Hint
Take the quotes away so JavaScript calculates.
A solution
console.log(365 * 24);11Problem
The empty string
console.log(typeof "");Hint
Two quotes with nothing between them are still quotes.
Solution
Answer: string
Two quotes with nothing between them make the empty string, which is still a string.
12Explain
Common mistakes
console.log(Age:, 30);SyntaxError: missing ) after argument list
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- The line number shows where the program stopped. A SyntaxError stops it before any line runs.
Text needs quotes. Without them, Age: is not valid JavaScript, so the whole program is rejected before it runs.
console.log("Total:", Total);ReferenceError: Total is not defined (line 1)
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- The line number shows where the program stopped. A SyntaxError stops it before any line runs.
Total without quotes is read as a name, and no variable has that name.
13
Summary
- 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.