Working with console in JavaScript

For the convenience of programmers, each browser has a special developer panel. You need to right-click anywhere on the current site page to open this panel. In the menu appeared, you need to select the lowest item (it will be called Inspect or something like that).

In the panel that opens, you will mainly use two tabs: Elements and Console. In the first tab you can get information about page tags and in the second one JavaScript debug information.

Let's understand the possibilities of the console.

With your scripts you can output data to the console, using the special command console.log. This is used to debug programs.

As an example, let's output something to the console:


Now let's output the variable value:

let num = 123; console.log(num);

You can output multiple variables in turn:

let num1 = 123; let num2 = 456; console.log(num1); console.log(num2);

You can display multiple variables with single command by writing these variables separated by commas:

let num1 = 123; let num2 = 456; console.log(num1, num2);

Given a variable. Output its value to the console .

Three variables are given. Output their values to the console.

Data type in the console

The console displays strings and numbers in different colors. This often helps to find errors in programs.

Let's print a number and a string side by side so we can see the difference in color:

console.log(123); console.log('123');

Print all the data types you know to the console. See how their colors differ.

Errors in the console

When you make a syntax error in your code, JavaScript will print information about it to the console. Such an error will be displayed in red, and to the right of it the line number, on which this error occurred, will be indicated. This number is a link you can click on, and your code will be opened in the console on the line with the error.

Let's try it in practice. Let's output a non-existent variable. In this case, JavaScript will throw an error:

alert(eee); // Uncaught ReferenceError: eee is not defined

Make a mistake on purpose in your code. Verify that the error appears in the console. Determine the line of code where the error occurred on.