Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Function() Constructor
Next article icon

Console in JavaScript

Last Updated : 23 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The console object provides access to the browser’s debugging console (or terminal in Node.js). It is used to log information, debug code, and interact with the runtime environment during development.

Commonly Used console Methods

Here are the most frequently used methods of the console object:

1. console.log()

The console.log() function logs general information to the console. This is one of the most commonly used methods for debugging in JavaScript.

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

Output
Hello, World! 

2. console.error()

The console .error() function in JavaScript Logs error messages to the console. Typically displayed in red. It is used to display an error message on the console.

JavaScript
console.error("This is an error message."); 
consoleerror

console .error()

3. console .warn()

The console. warn() function Logs warnings on to the console to warn the user about certain scenarios, typically displayed in yellow.

JavaScript
console.warn("This is a warning message."); 
consolewarn

console. warn()

4. console.info()

The console.info() logs informational messages, which usually appear as standard logs but can be styled differently in some environments. Using the %c flag and passing the style object as the second parameter to the function can be used to style the info message.

JavaScript
console.info('%cThis is a styled info message!', 'color: blue; font-size: 16px; font-weight: bold;'); 
hello

console.info()

5. console .table()

console.table() is a method that displays data in a structured table format, making it easier to read and compare. It takes arrays or objects and organizes their properties or elements into rows and columns, helping with debugging or analyzing large or complex datasets.

JavaScript
console.table([{name: "Amit", age: 30}, {name: "Jatin", age: 25}]); 

Output
┌─────────┬─────────┬─────┐ │ (index) │  name   │ age │ ├─────────┼─────────┼─────┤ │    0    │ 'Amit'  │ 30  │ │    1    │ 'Jatin' │ 25  │ └─────────┴─────────┴─────┘ 

6. console .time() & console .timeEnd()

The console.time() method starts a timer with a specified label, and console.timeEnd() stops the timer and logs the elapsed time in milliseconds. These methods are useful for measuring how long a block of code takes to execute.

JavaScript
console.time('timer1'); function loops() {     for (let i = 0; i <= 10000; i++) {      } } loops() console.timeEnd('timer1');  

Output
timer1: 0.278ms 

7. console.assert()

console.assert() logs an error message if the given condition is false, helping to catch issues during development. If the condition is true, it produces no output.

JavaScript
console.assert(5 > 10, "This assertion failed"); 

8. console .group() and console.groupEnd()

The console.group() and console.groupEnd() methods let you group related logs together in the console. This makes it easier to organize and read logs, especially when debugging.

JavaScript
console.group('User Information'); console.log('Name: Ritik'); console.log('Age: 30'); console.groupEnd(); 

Output
User Information   Name: Ritik   Age: 30 

9. console.count()

console.count() logs the number of times it has been called with a specific label. It helps track how many times a particular code block or function is executed during runtime.

JavaScript
console.count("countLabel");   console.count("countLabel");  console.count("countLabel"); console.count("countLabel");  

Output
countLabel: 1 countLabel: 2 countLabel: 3 countLabel: 4 

10. console.trace()

The console.trace() method outputs a stack trace to the console, showing the path your code took to reach the point where it was called. This helps track the flow of execution and identify where a function was invoked.

JavaScript
function a() {     b() } function b() {     c() } function c() {     console.trace() } a() 


Next Article
JavaScript Function() Constructor
author
immukul
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-basics

Similar Reads

  • Functions in JavaScript
    Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs. [GFGTABS] JavaScript function sum(x, y) { return x + y; } console.log(sum(6, 9)); [/GFGTABS]Output1
    5 min read
  • Code Golfing in JavaScript
    Code Golf in JavaScript refers to attempting a problem to solve using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters "wins". JavaScript is a fantastic language for code golfing due to backward compatibility, quirks, it is being a high-level
    4 min read
  • JavaScript Comments
    Comments help explain code (they are not executed and hence do not have any logic implementation). We can also use them to temporarily disable parts of your code. 1. Single Line CommentsA single-line comment in JavaScript is denoted by two forward slashes (//), [GFGTABS] JavaScript // A single line
    2 min read
  • JavaScript Function() Constructor
    The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
    2 min read
  • JavaScript console.log() Method
    The console.log() method in JavaScript logs messages or data to the console. The console.log() method is useful for debugging or testing purposes. Syntax:console.log("");Parameters: Any message either number, string, array object, etc. Return value: It returns the value of the parameter given. Using
    2 min read
  • Control Statements in JavaScript
    JavaScript control statement is used to control the execution of a program based on a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition. Types of Control Statements
    3 min read
  • JavaScript Basics
    JavaScript is a versatile, lightweight scripting language widely used in web development. It can be utilized for both client-side and server-side development, making it essential for modern web applications. Known as the scripting language for web pages, JavaScript supports variables, data types, op
    6 min read
  • Debugging in JavaScript
    Debugging is the process of testing, finding, and reducing bugs (errors) in computer programs. It involves: Identifying errors (syntax, runtime, or logical errors).Using debugging tools to analyze code execution.Implementing fixes and verifying correctness.Types of Errors in JavaScriptSyntax Errors:
    4 min read
  • JavaScript Events
    JavaScript Events are actions or occurrences that happen in the browser. They can be triggered by various user interactions or by the browser itself. [GFGTABS] HTML <html> <script> function myFun() { document.getElementById( "gfg").innerHTML = "GeeksforGeeks"; } </
    3 min read
  • JavaScript get Function
    JavaScript get function is used to access the properties of an object using dot notation or square brackets. It allows you to retrieve the value associated with a particular property key and the get function is often used when working with objects that implement JavaScript's getter function. The get
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences