Course
Console.log()
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Console.log()
JavaScript console.log() method
In JavaScript, the
console.log()
method is used to print messages or values to the console. This can be helpful for debugging and understanding how your code is running.Syntax
The basic syntax of the
console.log()
method is:1console.log(value1, value2,..., valueN);
You can pass one or more values as arguments to
console.log()
, which will be printed in the console.Console.log() with client-sided JavaScript
Whenever we use the
console.log()
method in the frontend code, it prints the output in the browser's console.Here is shortcut to open the console in Chrome browser
Press Ctrl + Shift + J together on Windows/Linux and Cmd + Option + J on Mac.
Most browsers have the same shortcut key to open the console. However, if you can’t open the console in any browser, you may search on Google.
Example
In the example below, we have printed the "Hello World!" message using the
console.log()
method.Also, we defined two integer variables and used another
console.log()
method to print the sum of num1 and num2. Here, developers can observe how we have printed variables in the console. The example also demonstrates that we can perform addition, multiplication, etc. operations in the console.log()
method parameter.<html><head> <title> Console.log() method with HTML </title></head><body> <h3> Message is printed in the console </h3> <p> Please open the console before clicking "Edit & Run" button </p> <script> console.log("Hello World!"); var num1 = 30; var num2 = 20; console.log("The sum of ", num1, " and ", num2, " is: ", num1 + num2);</script></body></html>
It will produce the following result in the Console
Hello World!The sum of 30 and 20 is: 50