2. Printing#

One of the first things you’ll want to do in JavaScript is display information on the console.

JavaScript provides a simple method to print messages to the console: console.log().

Try running the code below and play around with the message.

console.log("Hello from JavaScript!");

The syntax for console.log() is:

console.log(message);

Where:

  • console.log must be lowercase

  • message is the object to be printed, for example a string.

2.1. More Examples!#

Printing Numbers

console.log(42);

Printing Variables - More about variables on the next pages!

let person = "Alice";
console.log(person);

Combining Strings

console.log("I am " + 17 + " years old.");

2.2. Common Printing Mistakes#

Strings require quotes: The following will cause an error.

console.log(Hello);

Missing Parentheses: The following will cause an error.

console.log "Hello";

Typos: JavaScript is case-sensitive, so Console.log() or console.Log() will not work. The following will cause an error.

Console.log("Hello")
console.Log("Hello")

2.3. Why Do We Use console.log()?#

When learning JavaScript, you might wonder why we use the full console.log() instead of just a simpler, single function like print in Python.

In JavaScript the console or terminal is represented by the globally available object console, which has many ways to show information. For example:

  • console.log(): displays general information.

  • console.warn(): displays a warning message.

  • console.error(): displays an error message.

The different types of printing reflect common situations that a browser encounters, such as a web developer using a deprecated function on a page, which might be presented as a warning. Likewise if something on the page isn’t compliant or crashes it should be shown as an error on the console.