console.log()

Introduction

The console.log() method is one of the most commonly used functions in JavaScript. It is used to display information, variable values, objects, arrays, and debugging messages in the browser’s Developer Console or the Node.js terminal.

For beginners, console.log() is the primary way to understand how JavaScript programs work. For automation engineers, it is an essential debugging tool that helps verify data, inspect API responses, troubleshoot test scripts, and monitor program execution.


What is console.log()?

The console.log() method prints output to the console.

It is mainly used for:

  • Displaying text

  • Displaying variable values

  • Debugging code

  • Printing objects and arrays

  • Verifying API responses

  • Tracking program execution


Syntax

console.log(value);

You can also pass multiple values:

console.log(value1, value2, value3);

Why Do We Use console.log()?

The console.log() method helps developers:

  • Check program output.

  • Debug JavaScript code.

  • Verify variable values.

  • Display API responses.

  • Inspect objects and arrays.

  • Understand program flow.


Printing Text

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

Output

Hello, JavaScript!

Printing Numbers

console.log(100);

Output

100

Printing Variables

let username = "John";

console.log(username);

Output

John

Printing Multiple Values

let name = "Alice";
let age = 25;

console.log(name, age);

Output

Alice 25

Printing Expressions

let x = 10;
let y = 5;

console.log(x + y);

Output

15

Printing Strings with Variables

let city = "Mumbai";

console.log("City:", city);

Output

City: Mumbai

Printing Objects

const student = {
    name: "Rahul",
    age: 22,
    course: "JavaScript"
};

console.log(student);

Output

{
  name: "Rahul",
  age: 22,
  course: "JavaScript"
}

The browser console allows you to expand the object to view all its properties.


Printing Arrays

const fruits = ["Apple", "Banana", "Orange"];

console.log(fruits);

Output

["Apple", "Banana", "Orange"]

Printing Boolean Values

console.log(true);
console.log(false);

Output

true
false

Printing null and undefined

let value;

console.log(value);
console.log(null);

Output

undefined
null

Real-World Example

Suppose you are developing a login page.

let username = "admin";

console.log("Logged in user:", username);

Output

Logged in user: admin

This helps verify which user has logged into the application.


Another example:

let totalPrice = 1250;

console.log("Total Price:", totalPrice);

Output

Total Price: 1250

Automation Testing Example

Automation engineers frequently use console.log() to inspect API responses.

const response = {
    status: 200,
    message: "Login Successful"
};

console.log(response);

Output

{
  status: 200,
  message: "Login Successful"
}

Printing a specific property:

const response = {
    status: 200,
    message: "Login Successful"
};

console.log(response.message);

Output

Login Successful

Example with Playwright:

const pageTitle = await page.title();

console.log(pageTitle);

Output

Home Page

This helps verify that the correct page has loaded.


Example with API testing:

const statusCode = 200;

console.log("Status Code:", statusCode);

Output

Status Code: 200

console.log() in Browser

When JavaScript runs inside a browser, the output appears in the Developer Console.

Example:

console.log("Welcome to JavaScript");

Open the browser’s Developer Tools (usually by pressing F12) and select the Console tab to view the output.


console.log() in Node.js

When JavaScript runs using Node.js, the output appears directly in the terminal.

Example:

console.log("Running JavaScript using Node.js");

Output

Running JavaScript using Node.js

Common Mistakes

Forgetting Parentheses

Incorrect:

console.log;

Correct:

console.log("Hello");

Misspelling the Method Name

Incorrect:

Console.log("Hello");

JavaScript is case-sensitive.

Correct:

console.log("Hello");

Forgetting Quotes Around Strings

Incorrect:

console.log(Hello);

This produces a ReferenceError because JavaScript treats Hello as a variable.

Correct:

console.log("Hello");

Leaving Debug Statements in Production Code

Avoid leaving unnecessary console.log() statements in production applications, as they can clutter the console and expose internal information.


Best Practices

Use Meaningful Messages

Instead of:

console.log(value);

Use:

console.log("User ID:", value);

Use console.log() for Debugging

Display variable values to understand how your program is executing.


Remove Unnecessary Logs

Delete or comment out temporary debugging statements before deploying your application.


Use Other Console Methods When Appropriate

JavaScript also provides:

  • console.error() – Displays error messages.

  • console.warn() – Displays warning messages.

  • console.table() – Displays data in a table format.

These methods improve readability during debugging.


Conclusion

The console.log() method is one of the most important tools for JavaScript developers. It allows you to display output, inspect variables, debug programs, and verify application behavior.

For automation engineers, console.log() is invaluable for validating API responses, checking test data, monitoring execution flow, and troubleshooting automation scripts. Mastering this simple method will make debugging and development much easier.


Frequently Asked Questions (FAQs)

What is console.log() in JavaScript?

console.log() is a method used to display output in the browser’s Developer Console or the Node.js terminal.


What is the syntax of console.log()?

console.log(value);

Can console.log() print multiple values?

Yes.

Example:

console.log("Name:", "John", "Age:", 25);

Where does the output of console.log() appear?

  • In the Developer Console when running JavaScript in a browser.

  • In the terminal when running JavaScript with Node.js.


Why is console.log() important in automation testing?

Automation engineers use console.log() to inspect variables, validate API responses, debug test scripts, verify application behavior, and identify issues during test execution.


Key Takeaways

  • console.log() displays output in the console.

  • It is widely used for debugging JavaScript programs.

  • It can print strings, numbers, variables, objects, arrays, and Boolean values.

  • Multiple values can be displayed in a single statement.

  • The output appears in the browser console or Node.js terminal.

  • It helps developers understand program execution.

  • It is commonly used in API testing and automation frameworks.

  • Use meaningful messages for better debugging.

  • Remove unnecessary console.log() statements before production deployment.

  • Mastering console.log() is essential for every JavaScript developer and automation engineer.