What printing does
A program is more useful when you can see what it computes. Printing writes a value to the console, the text output of the program. In TypeScript you print with console.log. You write the name, an opening parenthesis, the value you want to show, a closing parenthesis, and a semicolon:
console.log(1998);
Output:
1998
The value between the parentheses is called an argument. Each call to console.log prints its argument and then moves to a new line, so consecutive calls produce consecutive lines.
TypeScript has no print function of its own. console is an object provided by the environment that runs your code, and log is one of its methods. The TypeScript compiler knows about it and checks that your call is well formed, but the printing itself happens when the compiled JavaScript runs.
Numbers and strings look alike on screen
A number is written as it is. A string, which is text, goes inside quotes. Both of these print the same characters:
console.log(1998);
console.log("1998");
1998
1998
The output hides a difference that matters as soon as you compute with the values. The first is a number and the second is a string:
console.log(1998 + 2);
console.log("1998" + 2);
console.log(typeof 1998, typeof "1998");
2000
19982
number string
With two numbers, + adds. With a string on one side, + joins the text, so the 2 becomes the character "2" glued to the end. This is why the exercise insists that a number needs no quotes.
Printing several things at once
console.log accepts any number of arguments, separated by commas. It prints them on one line with a single space between each:
console.log("Age:", 31);
console.log("a", "b", 3, true);
Age: 31
a b 3 true
Calling it with no arguments, or with an empty string, prints an empty line:
console.log("first");
console.log();
console.log("third");
This prints first, then a blank line, then third.
Template literals
To build one message from several pieces, use backticks and put expressions inside ${}:
console.log(`2 + 3 = ${2 + 3}`);
2 + 3 = 5
Arrays and objects
You can print whole arrays and objects. The console formats them in a readable way:
console.log([1, 2, 3]);
console.log({ id: 7, ok: true });
[ 1, 2, 3 ]
{ id: 7, ok: true }
The exact formatting can differ between environments, but the contents are the same.
Order and semicolons
Statements run from top to bottom, and each console.log prints at the moment it runs. If you print a variable before you change it, you see the old value; after the change, the new one. Printing is the simplest way to follow what a program is doing, so you will use it constantly while learning.
The semicolon ends a statement. TypeScript can often work out where a statement ends without it, but ending every statement with one is a clear habit and avoids rare surprises. Note also that console.log only shows a value. It does not give anything back for later use, so you cannot store its result in a variable and expect the printed text.
Common mistakes
- Wrong spelling or capital letters.
Console.log,console.Logandconsle.logare all errors. TypeScript is case sensitive. - Forgetting the parentheses.
console.log 1998;is not a call. The parentheses are what call the function. - Quotes around a number you want to compute with. It prints the same, but behaves as text.
- Unbalanced quotes or parentheses. Every opening one needs a closing partner. The Syntax Errors lesson covers these.
- Quoting a name you meant to print.
console.log("total")prints the word total. To print the value of a variable namedtotal, writeconsole.log(total).
The exercise
The exercise asks you to print the number 1998. There is one step:
- Write
console.log. - Open a parenthesis and write
1998with no quotes, since it is a number. - Close the parenthesis and end the statement with a semicolon.
The lesson's tests look at the console output, so the line must print exactly 1998.
Key takeaways
console.log(value)prints the value and ends the line.- Numbers are written bare, and strings go in quotes. They print alike but behave differently.
- Several arguments print on one line, separated by spaces.
- Template literals with
${}build messages from expressions.