edubun
English
1 / 13

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 questions

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

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

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");   // string
Output
number
string
boolean
string

3Explore

Check any value

Pick a value. Quotes always make a string, even "42" and "true"; the second line shows how the value prints.

value
console.log(typeof 42);
console.log(42);
Output
number
42

4Problem

The type of "7.5"

What does this print?
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 + 3
Output
5
2 + 3

6Problem

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 units
Output
Age: 30
Sum: 9 units

8Problem

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.

Expected output
number
string
Your code
console.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.

Expected output
8760
Your code
console.log("365 * 24");
Hint

Take the quotes away so JavaScript calculates.

A solution
console.log(365 * 24);

11Problem

The empty string

What does this print?
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);
Error message
SyntaxError: missing ) after argument list
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. 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);
Error message
ReferenceError: Total is not defined (line 1)
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. 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.

Next unitVariables: let and const
Full text · The whole lesson as one text

Everything a program works with is a value

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

When output looks right but a later calculation fails, check the type before changing the arithmetic. A printed string can resemble a printed number, so appearance alone does not tell you what the program holds. Print the value and its type together. Then return to the line that created the value and check whether quotes made it text. This separates a data problem from a calculation problem.

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.

Practice questions from Values and console.log on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (2)
  • MDN Web Docs, JavaScript Guide (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, chapters Introduction, Grammar and types, Control flow, Loops and iteration, Functions, Expressions and operators, Numbers and strings, Indexed collections, Working with objects; checked 2026-10-17)
  • Ecma International, ECMA-262 ECMAScript 2026 Language Specification, 17th edition, June 2026 (tc39.es/ecma262 and ecma-international.org/publications-and-standards/standards/ecma-262, checked 2026-10-17).

Checked: 2026-10-17