edubun 한국어

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

TypeExamplesUsed for
number42, 3.5, -7Counting, measuring, money
string"cat", 'Hello', "42"Text of any kind
booleantrue, falseYes or no answers
undefinedundefinedA value that has not been set yet
nullnullDeliberately 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");   // string

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 + 3

A 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 units

This 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.

valueOne piece of data: 42, "cat", true
typeThe kind of value: number, string, boolean and so on
numberWhole numbers and decimals, one type for both
stringText written between quotes
booleantrue or false, written without quotes
expressionCode that produces a value, such as 2 + 3
typeofGives the type of a value as a string
empty stringA string with no characters, written ""
  1. What does this print?
    console.log(6 * 7);
  2. What does this print?
    console.log("6 * 7");
    • 42
    • 6 * 7
    • "6 * 7"
  3. What does this print?
    console.log(typeof 7.5);
  4. What does this print?
    console.log(typeof "7.5");
  5. What does this print?
    console.log(typeof false);
  6. What does this print?
    console.log("Total:", 3 + 4);
    • Total: 7
    • Total:7
    • Total: 3 + 4
  7. Which of these is a boolean value?
    • "true"
    • true
    • 1
  8. What does this print?
    console.log(typeof "");
  9. What does this print?
    console.log(10 - 4, "10 - 4");
    • 6 6
    • 6 10 - 4
    • 10 - 4 10 - 4
  10. Which one is a valid string?
    • "hello'
    • 'hello'
    • hello
Key points
  • 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.