Function console.log
This is an extension of the hands-on section, using a few simple methods to deepen your impression of JavaScript.
The console.log
method in JavaScript is used to print messages to the web console. This is helpful for debugging purposes, allowing developers to inspect the values of variables, track the flow of execution, and understand the state of the application at various points in time.
Using console.log
This code will output “Hello, World!” to the console.
Example with More Complex Data
This will output the person
object to the console, allowing you to inspect its properties.
Using Developer Tools in Firefox and Chrome
Chrome Developer Tools
-
Open Developer Tools:
- Press
Ctrl+Shift+I
(orCmd+Option+I
on Mac) or right-click on the page and select “Inspect”.
- Press
-
Navigate to the Console:
- Click on the “Console” tab to open the console.
-
Using
console.log
:- You can type JavaScript code directly into the console and press
Enter
to execute it. For example:
- You can type JavaScript code directly into the console and press
Firefox Developer Tools
-
Open Developer Tools:
- Press
Ctrl+Shift+I
(orCmd+Option+I
on Mac) or right-click on the page and select “Inspect Element”.
- Press
-
Navigate to the Console:
- Click on the “Console” tab to open the console.
-
Using
console.log
:You can type JavaScript code directly into the console and press
Enter
to execute it. For example:
HTML Example with console.log
Here is a complete HTML example that includes JavaScript code utilizing console.log
: