<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ console - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ console - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 10:49:57 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/console/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Console Logging Tips – How to Debug and Understand Your Code ]]>
                </title>
                <description>
                    <![CDATA[ Console logging is an essential tool for developers to use to debug and understand the behavior of their code.  While most developers are familiar with basic console logging using console.log(), there are many other powerful methods provided by the c... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/console-logging-tips/</link>
                <guid isPermaLink="false">66c4c3bf8e05d0e4345147bb</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ console ]]>
                    </category>
                
                    <category>
                        <![CDATA[ debugging ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joan Ayebola ]]>
                </dc:creator>
                <pubDate>Tue, 20 Feb 2024 18:09:59 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/Ivory-and-Blue-Lavender-Aesthetic-Photo-Collage-Presentation--9-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Console logging is an essential tool for developers to use to debug and understand the behavior of their code. </p>
<p>While most developers are familiar with basic console logging using <code>console.log()</code>, there are many other powerful methods provided by the console object that can make debugging more efficient and effective.</p>
<p>In this comprehensive guide, we will explore various console logging tricks such as <code>console.table</code>, <code>console.group</code>, <code>console.assert</code>, and more. These tricks can help you organize your debugging process, visualize complex data structures, and catch errors early on in your development workflow.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><a class="post-section-overview" href="#heading-1-introduction-to-console-logging">Introduction to Console Logging</a></li>
<li><a class="post-section-overview" href="#heading-2-basic-console-logging">Basic Console Logging</a></li>
<li><a class="post-section-overview" href="#heading-3-advanced-console-logging-tricks">Advanced Console Logging Tricks</a><br>– <code>[console.table](#heading-31-consoletable)</code><br>– <a class="post-section-overview" href="#heading-32-consolegroup-and-consolegroupcollapsed"><code>console.group</code>  and <code>console.groupCollapsed</code></a><br>– <code>[console.assert](#heading-33-consoleassert)</code><br>– <a class="post-section-overview" href="#heading-34-consolecount-and-consolecountreset"><code>console.count</code> and <code>console.countReset</code></a><br>– <a class="post-section-overview" href="#heading-35-consoletime-and-consoletimeend"><code>console.time</code> and <code>console.timeEnd</code></a><br>– <code>[console.trace](#heading-36-consoletrace)</code><br>– <code>[console.dir](#heading-37-consoledir)</code><br>– <code>[console.clear](#heading-38-consoleclear)</code></li>
<li><a class="post-section-overview" href="#heading-4-best-practices-for-console-logging">Best Practices for Console Logging</a></li>
<li><a class="post-section-overview" href="#heading-5-conclusion">Conclusion</a></li>
</ol>
<h2 id="heading-1-introduction-to-console-logging">1. Introduction to Console Logging</h2>
<p>Console logging is a technique used by developers to output messages, variables, and other information to the browser's console. This is particularly useful for debugging purposes, as it allows developers to inspect the state of their code and track its execution flow.</p>
<p>The <code>console</code> object in JavaScript provides various methods for logging different types of information. While <code>console.log()</code> is the most commonly used method, there are several other methods that can be used to enhance your debugging experience.</p>
<h2 id="heading-2-basic-console-logging">2. Basic Console Logging</h2>
<p>Before we dive into the advanced console logging tricks, let's start by revisiting the basics of console logging using <code>console.log()</code>. This method accepts any number of arguments and outputs them to the console.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> name = <span class="hljs-string">"Femi"</span>;

<span class="hljs-keyword">const</span> age = <span class="hljs-number">30</span>;

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Name:"</span>, name, <span class="hljs-string">"Age:"</span>, age);
</code></pre>
<p>In the above example, we are logging the <code>name</code> and <code>age</code> variables to the console using <code>console.log()</code>. This will output:</p>
<pre><code>Name: Femi Age: <span class="hljs-number">30</span>
</code></pre><p>You can use <code>console.log()</code> to log strings, numbers, booleans, objects, arrays, and more.</p>
<h2 id="heading-3-advanced-console-logging-tricks">3. Advanced Console Logging Tricks</h2>
<h3 id="heading-31-consoletable">3.1 <code>console.table</code></h3>
<p>The <code>console.table()</code> method allows you to display tabular data in the console. It takes an array or an object as input and presents it as a table.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> users = [

{ <span class="hljs-attr">name</span>: <span class="hljs-string">"Chris"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },

{ <span class="hljs-attr">name</span>: <span class="hljs-string">"Dennis"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">15</span> },

{ <span class="hljs-attr">name</span>: <span class="hljs-string">"Victor"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">17</span> }

];

<span class="hljs-built_in">console</span>.table(users);
</code></pre>
<p>The above code will output a table in the console:</p>
<pre><code class="lang-markdown"><span class="hljs-section">(index)  |  name  |  age
-------------------------</span>
0    |  Chris  |   25
1    |  Dennis |   15
2    |  Victor |   17
</code></pre>
<p><code>console.table()</code> is particularly useful when dealing with arrays of objects or other tabular data structures.</p>
<h3 id="heading-32-consolegroup-and-consolegroupcollapsed">3.2 <code>console.group</code> and <code>console.groupCollapsed</code></h3>
<p>The <code>console.group()</code> and <code>console.groupCollapsed()</code> methods allow you to group related log messages together in the console. This can help organize your debugging output and make it easier to understand.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Start a new console group named "Group 1"</span>
<span class="hljs-built_in">console</span>.group(<span class="hljs-string">"Group 1"</span>);

<span class="hljs-comment">// Log messages inside "Group 1"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message 1"</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message 2"</span>);

<span class="hljs-comment">// End "Group 1"</span>
<span class="hljs-built_in">console</span>.groupEnd();

<span class="hljs-comment">// Start a new collapsed console group named "Group 2"</span>
<span class="hljs-built_in">console</span>.groupCollapsed(<span class="hljs-string">"Group 2"</span>);

<span class="hljs-comment">// Log messages inside "Group 2"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message 3"</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message 4"</span>);

<span class="hljs-comment">// End "Group 2"</span>
<span class="hljs-built_in">console</span>.groupEnd();
</code></pre>
<p>In the above example, we create two groups of log messages. The first group is expanded, while the second group is collapsed by default. This helps keep the console output organized and easy to navigate.</p>
<p>If you run this code in a browser's developer console, the output will look something like this:</p>
<pre><code>Group <span class="hljs-number">1</span>
  Message <span class="hljs-number">1</span>
  Message <span class="hljs-number">2</span>
Group <span class="hljs-number">2</span>
  ▶ Message <span class="hljs-number">3</span>
  ▶ Message <span class="hljs-number">4</span>
</code></pre><p>In this example, "Group 1" is expanded by default, showing the messages inside it. On the other hand, "Group 2" is collapsed initially (indicated by the ▶ symbol), and you need to click on it to expand and reveal the messages inside. The collapsing of "Group 2" makes it visually neater in the console, especially when dealing with a large number of log messages.</p>
<h3 id="heading-33-consoleassert">3.3 <code>console.assert</code></h3>
<p>The <code>console.assert()</code> method allows you to assert whether a condition is true or false. If the condition is false, it will log an error message to the console.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> x = <span class="hljs-number">5</span>;

<span class="hljs-comment">// Check if the condition x === 10 is true, if not, log the error message</span>
<span class="hljs-built_in">console</span>.assert(x === <span class="hljs-number">10</span>, <span class="hljs-string">"x is not equal to 10"</span>);
</code></pre>
<p>In this case, the condition being checked is <code>x === 10</code>, which compares the value of the variable <code>x</code> to 10. Since the value of <code>x</code> is 5, the condition is false. As a result, the <code>console.assert</code> method will log the error message to the console.</p>
<p>If you run this code in a browser's developer console, you would see an assertion error in the console output with the specified error message:</p>
<pre><code>Assertion failed: x is not equal to <span class="hljs-number">10</span>
</code></pre><p>This is a helpful way to include runtime checks in your code and log informative messages if certain conditions are not met.</p>
<h3 id="heading-34-consolecount-and-consolecountreset">3.4 <code>console.count</code> and <code>console.countReset</code></h3>
<p>The <code>console.count()</code> method allows you to count the number of times a particular piece of code is executed. You can also reset the count using <code>console.countReset()</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Log and count the number of times "greet" is called</span>
  <span class="hljs-built_in">console</span>.count(<span class="hljs-string">"greet"</span>);

  <span class="hljs-comment">// Return a greeting message</span>
  <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello!"</span>;
}

<span class="hljs-comment">// Call greet() two times</span>
greet();
greet();

<span class="hljs-comment">// Reset the counter for "greet"</span>
<span class="hljs-built_in">console</span>.countReset(<span class="hljs-string">"greet"</span>);

<span class="hljs-comment">// Call greet() again</span>
greet();
</code></pre>
<p><code>console.count("greet");</code>: This line logs the number of times "greet" is called. The count is initially 1 when <code>greet()</code> is first called and increments with each subsequent call.</p>
<p>If you run this code in a browser's developer console, the output might look like this:</p>
<pre><code>greet: <span class="hljs-number">1</span>
<span class="hljs-attr">greet</span>: <span class="hljs-number">2</span>
<span class="hljs-attr">greet</span>: <span class="hljs-number">1</span>
</code></pre><p>The first two calls to <code>greet</code> increment the count, and the third call, after the reset, starts the count again from 1. The count is specific to the label "greet."</p>
<h3 id="heading-35-consoletime-and-consoletimeend">3.5 <code>console.time</code> and <code>console.timeEnd</code></h3>
<p>The <code>console.time()</code> and <code>console.timeEnd()</code> methods allow you to measure the time taken by a block of code to execute.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.time(<span class="hljs-string">"timer"</span>);

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000</span>; i++) {

<span class="hljs-comment">// Some time-consuming operation</span>

}

<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"timer"</span>);
</code></pre>
<p>In the above example,</p>
<ul>
<li><code>console.time("timer");</code>: This  starts a timer with the label "timer" when the loop </li>
<li><code>console.timeEnd("timer");</code>: This stops the timer labeled "timer" and logs the elapsed time to the console.</li>
</ul>
<p>If you run this code in a browser's developer console, the output will look like this:</p>
<pre><code>timer: XXms
</code></pre><p>The "XX" will be replaced with the actual time taken by the loop to execute the time-consuming operation. This measurement is useful for profiling and understanding the performance of a specific code block or operation.</p>
<h3 id="heading-36-consoletrace">3.6 <code>console.trace</code></h3>
<p>The <code>console.trace()</code> method outputs a stack trace to the console. This can be helpful for debugging purposes to see the call stack leading to the current execution point.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Call the bar function</span>
  bar();
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Log a trace of the call stack</span>
  <span class="hljs-built_in">console</span>.trace(<span class="hljs-string">"Trace:"</span>);
}

<span class="hljs-comment">// Call the foo function</span>
foo();
</code></pre>
<ul>
<li><code>foo</code> function: Calls the <code>bar</code> function.</li>
<li><code>bar</code> function: Logs a trace of the call stack using <code>console.trace</code>.</li>
<li><code>foo</code> is called: This triggers the call to <code>bar</code>, and the trace is logged.</li>
</ul>
<p>If you run this code in a browser's developer console, the output might look something like this:</p>
<pre><code>Trace:
bar @ (index):<span class="hljs-number">8</span>
foo @ (index):<span class="hljs-number">3</span>
(anonymous) @ (index):<span class="hljs-number">12</span>
</code></pre><p>The output shows the call stack at the time <code>console.trace</code> was called. It includes information about the functions in the stack, such as the function names and their respective locations in the code. In this example, the call stack is displayed in reverse order, with the most recent function call at the top.</p>
<h3 id="heading-37-consoledir">3.7 <code>console.dir</code></h3>
<p>The <code>console.dir()</code> method allows you to display an interactive listing of the properties of a JavaScript object.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">name</span>: <span class="hljs-string">"Chris"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> };

<span class="hljs-comment">// Display an interactive listing of the properties of the object</span>
<span class="hljs-built_in">console</span>.dir(obj);
</code></pre>
<p>The <code>console.dir</code> method is commonly used to log an interactive representation of an object to the console. If you run this code in a browser's developer console, the output might look something like this:</p>
<pre><code><span class="hljs-built_in">Object</span>
  <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>
  <span class="hljs-attr">name</span>: <span class="hljs-string">"Chris"</span>
  <span class="hljs-attr">__proto__</span>: <span class="hljs-built_in">Object</span>
</code></pre><p>This output provides a visual representation of the object's properties, including their names and values. It also shows the prototype of the object (<code>__proto__</code>). The <code>console.dir</code> method is particularly useful when dealing with complex objects or nested structures, as it allows you to explore the object's properties in a more interactive way than <code>console.log</code>.</p>
<h3 id="heading-38-consoleclear">3.8 <code>console.clear</code></h3>
<p>The <code>console.clear()</code> method clears the console of all previous log messages.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message 1"</span>);

<span class="hljs-built_in">console</span>.clear();

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Message 2"</span>);
</code></pre>
<p>In the above example, <code>console.clear()</code> will clear the console before logging "Message 2".</p>
<h2 id="heading-4-best-practices-for-console-logging">4. Best Practices for Console Logging</h2>
<p>While console logging can be a powerful debugging tool, it's important to use it judiciously and follow best practices:</p>
<ul>
<li><strong>Avoid Excessive Logging</strong>: Too many log messages can clutter the console and make it difficult to find relevant information. Only log what is necessary for debugging.</li>
<li><strong>Use Descriptive Messages</strong>: When logging messages, use descriptive labels to make it clear what each message represents.</li>
<li><strong>Use Console Methods Wisely</strong>: Choose the appropriate console method (<code>log</code>, <code>table</code>, <code>group</code>, and so on) based on the type of data you are logging and how you want it to be displayed.</li>
<li><strong>Remove Debugging Code in Production</strong>: Remember to remove or disable console logging statements in your production code to avoid unnecessary overhead.</li>
</ul>
<h2 id="heading-5-conclusion">5. Conclusion</h2>
<p>Console logging is a powerful tool for debugging JavaScript code. By leveraging advanced console logging tricks such as <code>console.table</code>, <code>console.group</code>, <code>console.assert</code>, and others, you can streamline your debugging process and gain deeper insights into your code's behavior.</p>
<p>In this comprehensive guide, we covered various console logging tricks, along with examples demonstrating how to use them effectively. By incorporating these techniques into your development workflow and following best practices, you can become a more efficient and effective developer.</p>
<p>Experiment with these console logging tricks in your own projects to see how they can help you debug and understand your code better. Happy debugging!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Work with the Console Object in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ When you run into errors inside your code, it's important to know how to debug and find the source of the problem. The console object is a powerful tool that can help you with this. In this article, we will explore the console object and the variety ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-work-with-the-console-object-in-javascript/</link>
                <guid isPermaLink="false">66b8d9bbbb00d99843c0c43b</guid>
                
                    <category>
                        <![CDATA[ console ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Jessica Wilkins ]]>
                </dc:creator>
                <pubDate>Wed, 07 Feb 2024 21:29:48 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/computer-on-desk.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you run into errors inside your code, it's important to know how to debug and find the source of the problem. The <code>console</code> object is a powerful tool that can help you with this.</p>
<p>In this article, we will explore the <code>console</code> object and the variety of methods that will help you debug your code.</p>
<h2 id="heading-what-is-the-console-object">What is the <code>console</code> Object?</h2>
<p>The <code>console</code> object is a global object that provides access to the browser's debugging console. This object has a variety of methods that can be used to log messages, errors, warnings and other information to the console.</p>
<p>In this article, we will look at the more commonly used methods such as <code>console.log</code>, <code>console.warn</code>, and <code>console.error</code>.</p>
<h2 id="heading-how-to-access-the-console-in-your-browser">How to Access the Console in Your Browser</h2>
<p>To access the console in your browser, you can right click on the page and select <code>Inspect</code>. This will open the developer tools. You can also use the keyboard shortcut <code>Ctrl + Shift + I</code> on Windows or <code>Cmd + Option + I</code> on Mac.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-06-at-7.50.03-PM.png" alt="Console tab in Google Chrome" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-use-the-consolelog-method">How to Use the <code>console.log()</code> Method</h2>
<p>The most commonly used method to debug applications is the <code>console.log</code> method. This method is used to log messages and variables to the console.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, world!"</span>);
</code></pre>
<p>When you run this code in your browser, you will see the message "Hello, world!" logged to the console.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-06-at-7.51.44-PM.png" alt="Console statement showing Hello, World" width="600" height="400" loading="lazy"></p>
<p>This method is also useful for logging the value of variables to the console and finding bugs in your code.</p>
<p>Let's say we wanted to build out a Mad Libs generator. This is a popular game where you fill in the blanks of a story with random words.</p>
<p>Here is the code we have created so far:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">madLibsGenerator</span>(<span class="hljs-params">verb, adjective, noun</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">`We shall <span class="hljs-subst">${verb}</span> the<span class="hljs-subst">${adjective}</span><span class="hljs-subst">${noun}</span>.`</span>;
}

<span class="hljs-built_in">console</span>.log(madLibsGenerator(<span class="hljs-string">"dance"</span>, <span class="hljs-string">"big"</span>, <span class="hljs-string">"dog"</span>));
</code></pre>
<p>We want to see what our function is doing, so we have a <code>console.log(madLibsGenerator("dance", "big", "dog"));</code>. But when we check the console, we notice some spacing issues with the printed sentence.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-06-at-8.05.45-PM.png" alt="Print statement for Mad Libs generator" width="600" height="400" loading="lazy"></p>
<p>Now that we know where the issue is, we can fix those spacing issues in the function here:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">madLibsGenerator</span>(<span class="hljs-params">verb, adjective, noun</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">`We shall <span class="hljs-subst">${verb}</span> the <span class="hljs-subst">${adjective}</span> <span class="hljs-subst">${noun}</span>.`</span>;
}

<span class="hljs-built_in">console</span>.log(madLibsGenerator(<span class="hljs-string">"dance"</span>, <span class="hljs-string">"big"</span>, <span class="hljs-string">"dog"</span>));
</code></pre>
<p>Now the console should print the correct result.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-06-at-8.07.44-PM.png" alt="Correct Mad Libs result" width="600" height="400" loading="lazy"></p>
<h2 id="heading-what-is-the-consolewarn-method">What is the <code>console.warn()</code> Method?</h2>
<p>The <code>console.warn</code> method is used to log warning messages to the console. This method is useful for logging messages that are not errors, but are still important for the developer to be aware of.</p>
<p>For example, if you are building an application that is using a deprecated method, you can use the <code>console.warn</code> method to log a warning message to the console.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.warn(
  <span class="hljs-string">"This method is deprecated and will be removed in the next version"</span>
);
</code></pre>
<p>When you run this code in your browser, you will see the warning message <code>"This method is deprecated and will be removed in the next version"</code> logged to the console.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-06-at-8.08.44-PM.png" alt="Console.warn method example" width="600" height="400" loading="lazy"></p>
<h2 id="heading-what-is-the-consoleerror-method">What is the <code>console.error()</code> Method?</h2>
<p>The <code>console.error</code> method is used to log error messages to the console. This method is useful for logging messages that indicate an error has occurred in your application.</p>
<p>Let's say that you have an application that is trying to fetch data from an API, but the API is down. You can use the <code>console.error</code> method to log an error message to the console.</p>
<p>Here is an example of how to use the <code>console.error</code> method to log an error message to the console:</p>
<pre><code class="lang-javascript">fetch(<span class="hljs-string">"https://api.example.com/data"</span>)
  .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json())
  .then(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(data))
  .catch(<span class="hljs-function">(<span class="hljs-params">error</span>) =&gt;</span>
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"There was an error fetching the data"</span>, error)
  );
</code></pre>
<p>If there is an error fetching the data from the API, you will see the error message <code>"There was an error fetching the data"</code> logged to the console.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Screenshot-2024-02-06-at-8.09.28-PM.png" alt="Example of failed error message" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>There are so many more methods that you can use with the <code>console</code> object to help you debug your code. In this article, we have only scratched the surface.</p>
<p>To learn more about the <code>console</code> object and the variety of methods that you can use, check out the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Console">MDN docs</a>.</p>
<p>I encourage you to play around with the <code>console</code> object and the variety of methods that it has to offer. It is a powerful tool that will help you debug your code and find the source of any issues that you may run into.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Make your Console Output Fun and Interactive in JavaScript and Node.js ]]>
                </title>
                <description>
                    <![CDATA[ By Vasyl Lagutin In this tutorial, you'll learn how to add a randomized delay to the console.log statements in JavaScript and Node.js. Why would you want to do this? First of all, programming should be fun. And making a boring thing like console.log... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-your-console-log-statements-look-fun-and-interactive/</link>
                <guid isPermaLink="false">66d45e04230dff01669057b3</guid>
                
                    <category>
                        <![CDATA[ console ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node js ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Feb 2021 21:11:25 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/60190c1a0a2838549dcbcd11.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vasyl Lagutin</p>
<p>In this tutorial, you'll learn how to add a randomized delay to the <code>console.log</code> statements in JavaScript and Node.js.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/ezgif.com-gif-maker.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-why-would-you-want-to-do-this">Why would you want to do this?</h2>
<p>First of all, programming should be fun. And making a boring thing like <code>console.log</code> look nice is very pleasing.</p>
<p>If you want to get quick access to the source code, you can check out this <a target="_blank" href="https://github.com/AgileNix/funkylog/">GitHub repository</a>.</p>
<h2 id="heading-step-1-create-a-function-that-takes-the-string-and-passes-it-into-consolelog">Step 1: Create a function that takes the string and passes it into console.log</h2>
<p>To make sure that every step is clear, we'll start small and create a function that accepts a string as a parameter and logs it to the console.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> log = <span class="hljs-function">(<span class="hljs-params">s</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(s);
}
</code></pre>
<h2 id="heading-step-2-log-characters-of-the-string-one-by-one">Step 2: Log characters of the string one-by-one</h2>
<p>Before we can add a delay between the output of the individual chars, we need to make sure that they're actually split.</p>
<p>Let's add a <code>for</code> loop that iterates over each letter of the string and prints it to the console.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> log = <span class="hljs-function">(<span class="hljs-params">s</span>) =&gt;</span> {
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> c <span class="hljs-keyword">of</span> s) {
    <span class="hljs-built_in">console</span>.log(c);
  }
}
</code></pre>
<h2 id="heading-step-3-how-to-fix-the-newline-issue">Step 3: How to fix the newline issue</h2>
<p>Now, each letter is printed on a new line as every call to <code>console.log</code> adds an empty line.</p>
<p>We'll replace the <code>console.log</code> with <code>_**process**_.stdout.write</code> which essentially does the same thing, but doesn't add a new line after the output.</p>
<p>Now, however, we've lost the newline in the very end of the output, which is still desirable. We'll add it by explicitly printing the <code>\n</code> character.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> log = <span class="hljs-function">(<span class="hljs-params">s</span>) =&gt;</span> {
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> c <span class="hljs-keyword">of</span> s) {
    process.stdout.write(c);
  }
  process.stdout.write(<span class="hljs-string">'\n'</span>);
}
</code></pre>
<h2 id="heading-step-4-implement-the-sleep-function">Step 4: Implement the <code>sleep</code> function</h2>
<p>In JavaScript we can't simply stop the execution of the synchronous code for some amount of time. To make this happen, we need to write our own function. Let's call it sleep.</p>
<p>It should accept a single parameter <code>ms</code> and return a Promise that resolves after the delay of <code>ms</code> milliseconds.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> sleep = <span class="hljs-function">(<span class="hljs-params">ms</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> <span class="hljs-built_in">setTimeout</span>(resolve, ms));
};
</code></pre>
<h2 id="heading-step-5-add-the-delay">Step 5: Add the delay</h2>
<p>So, we're ready to add a delay to our output! We need a couple of things here:</p>
<ul>
<li>add a parameter <code>delay</code> to the function <code>log</code></li>
<li>make the function <code>log</code> asynchronous by adding the keyword <code>async</code></li>
<li>call a <code>sleep</code> function that will delay the next loop iteration by <code>delay</code> milliseconds</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> sleep = <span class="hljs-function">(<span class="hljs-params">ms</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> <span class="hljs-built_in">setTimeout</span>(resolve, ms));
};

<span class="hljs-keyword">const</span> log = <span class="hljs-keyword">async</span> (s, delay) =&gt; {
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> c <span class="hljs-keyword">of</span> s) {
    process.stdout.write(c);
    <span class="hljs-keyword">await</span> sleep(delay);
  }
  process.stdout.write(<span class="hljs-string">'\n'</span>);
}
</code></pre>
<h2 id="heading-step-6-implement-randomized-delay">Step 6: Implement randomized delay</h2>
<p>The output will look even better if we randomize the timing.</p>
<p>Let's add another boolean parameter <code>randomized</code> to the function <code>log</code>. If it's true, then the argument passed into <code>sleep</code> should be in the range from <code>0</code> to <code>delay</code> milliseconds.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> sleep = <span class="hljs-function">(<span class="hljs-params">ms</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> <span class="hljs-built_in">setTimeout</span>(resolve, ms));
};

<span class="hljs-keyword">const</span> log = <span class="hljs-keyword">async</span> (s, delay, randomized) =&gt; {
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> c <span class="hljs-keyword">of</span> s) {
    process.stdout.write(c);
    <span class="hljs-keyword">await</span> sleep((randomized ? <span class="hljs-built_in">Math</span>.random() : <span class="hljs-number">1</span>) * delay);
  }
  process.stdout.write(<span class="hljs-string">'\n'</span>);
}
</code></pre>
<p>I've used a ternary operator, but you can replace it with a regular <code>if</code> statement:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span> (randomized) {
  sleep(<span class="hljs-built_in">Math</span>.random * delay);
} <span class="hljs-keyword">else</span> {
  sleep(delay);
}
</code></pre>
<h2 id="heading-step-7-make-the-log-configurable">Step 7: Make the log configurable</h2>
<p>Right now, we've implemented pretty much everything we wanted to. But calling it isn't very clean as we have to pass the <code>delay</code> and randomization flag every time we want to print something to the console.</p>
<pre><code class="lang-javascript">log(<span class="hljs-string">'Hello, world!'</span>, <span class="hljs-number">100</span>, <span class="hljs-literal">true</span>);
log(<span class="hljs-string">'What\'s up?'</span>, <span class="hljs-number">100</span>, <span class="hljs-literal">true</span>);
log(<span class="hljs-string">'How are you?'</span>, <span class="hljs-number">100</span>, <span class="hljs-literal">true</span>);
</code></pre>
<p>It'd be nice if we could have a configurable log that could be called with a single parameter - a string that we want to output.</p>
<p>To do this, we'll have to rewrite our code. Here's the plan:</p>
<ul>
<li>wrap all current functionality into a single function <code>funkylog</code> that accepts an object with 2 fields, <code>delay</code> and <code>randomized</code></li>
<li><code>funkylog</code> should return the anonymous arrow function. Its implementation should be the same as <code>log</code>, that we've implemented on steps 1 through 6</li>
<li>parameters <code>delay</code> and <code>randomized</code> should be removed from the <code>log</code> function as now they'll be passed down from the <code>funkylog</code></li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> funkylog = <span class="hljs-function">(<span class="hljs-params">{ delay, randomized }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> sleep = <span class="hljs-function">(<span class="hljs-params">ms</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> <span class="hljs-built_in">setTimeout</span>(resolve, ms));
  };

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">async</span> (s) =&gt; {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> c <span class="hljs-keyword">of</span> s) {
      process.stdout.write(c);
      <span class="hljs-keyword">await</span> sleep((randomized ? <span class="hljs-built_in">Math</span>.random() : <span class="hljs-number">1</span>) * delay);
    }
    process.stdout.write(<span class="hljs-string">'\n'</span>);
  }
};
</code></pre>
<h2 id="heading-step-8-add-the-finishing-touch">Step 8: Add the finishing touch</h2>
<p>Let's take a look at what we've got:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> log = funkylog({ <span class="hljs-attr">delay</span>: <span class="hljs-number">100</span>, <span class="hljs-attr">randomized</span>: <span class="hljs-literal">true</span> });

log(<span class="hljs-string">'Hello, world!'</span>);
log(<span class="hljs-string">'What\'s up?'</span>);
log(<span class="hljs-string">'How are you?'</span>);
</code></pre>
<ul>
<li>We can create a configurable logger using the function <code>funkylog</code></li>
<li>We can select any delay we want</li>
<li>Using the logger doesn't require us to pass the <code>delay</code> every time we call it</li>
</ul>
<p>One more improvement that we can make is providing a default value for the <code>delay</code> parameter.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> funkylog = <span class="hljs-function">(<span class="hljs-params">{ delay = <span class="hljs-number">100</span>, randomized }</span>) =&gt;</span> {
    ..
    ..
</code></pre>
<p>So, now we can create the <code>funkylog</code> without any arguments and it will still work!</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> log = funkylog();

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hello, world!'</span>);
</code></pre>
<h2 id="heading-improvement-ideas">Improvement ideas</h2>
<p>As I've said from the very beginning, first of all, programming should be fun. Otherwise it'll become a routine and you won't enjoy doing it.</p>
<p>Do make further improvements to the <code>funkylog</code> and let me know what your results look like! For example, you can spice up the output by colorizing it. You can use the <code>npm</code> module <code>chalk</code> for it.</p>
<p>Then, once you have implemented different colors, you can add another flag that would add an additional delay between the words in the string.</p>
<p>Thank you for staying with me, throughout the whole tutorial!<br>I write a programming blog at <a target="_blank" href="https://learn.coderslang.com">learn.coderslang.com</a> and build a <a target="_blank" href="https://js.coderslang.com">Full Stack JS course</a>.</p>
<h3 id="heading-if-you-have-feedback-or-questions-on-this-tutorial-feel-free-to-tweet-me-coderslang-or-jump-into-the-discussion-on-telegram-coderslangchat">If you have feedback or questions on this tutorial, feel free to Tweet me <strong>@coderslang</strong> or jump into the discussion on Telegram <strong>@coderslang_chat</strong></h3>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Console.log() Example – How to Print to the Console in JS ]]>
                </title>
                <description>
                    <![CDATA[ Logging messages to the console is a very basic way to diagnose and troubleshoot minor issues in your code. But, did you know that there is more to console than just log? In this article, I'll show you how to print to the console in JS, as well as al... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-console-log-example-how-to-print-to-the-console-in-js/</link>
                <guid isPermaLink="false">66d45f668812486a37369cdf</guid>
                
                    <category>
                        <![CDATA[ console ]]>
                    </category>
                
                    <category>
                        <![CDATA[ debugging ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Jesse Hall ]]>
                </dc:creator>
                <pubDate>Wed, 09 Sep 2020 21:01:11 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/09/fCC_-Console.log.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Logging messages to the console is a very basic way to diagnose and troubleshoot minor issues in your code.</p>
<p>But, did you know that there is more to <code>console</code> than just <code>log</code>? In this article, I'll show you how to print to the console in JS, as well as all of the things you didn't know <code>console</code> could do.</p>
<h2 id="heading-firefox-multi-line-editor-console">Firefox Multi-line Editor Console</h2>
<p>If you've never used the multi-line editor mode in Firefox, you should give it a try right now!</p>
<p>Just open the console, <code>Ctrl+Shift+K</code> or <code>F12</code>, and in the top right you will see a button that says "Switch to multi-line editor mode". Alternatively, you can press <code>Ctrl+B</code>.</p>
<p>This gives you a multi-line code editor right inside Firefox.</p>
<h2 id="heading-consolelog">console.log</h2>
<p>Let's start with a very basic log example.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> x = <span class="hljs-number">1</span>
<span class="hljs-built_in">console</span>.log(x)
</code></pre>
<p>Type that into the Firefox console and run the code. You can click the "Run" button or press <code>Ctrl+Enter</code>.</p>
<p>In this example, we should see "1" in the console. Pretty straightforward, right?</p>
<h2 id="heading-multiple-values">Multiple Values</h2>
<p>Did you know that you can include multiple values? Add a string to the beginning to easily identify what it is you are logging.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> x = <span class="hljs-number">1</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"x:"</span>, x)
</code></pre>
<p>But what if we have multiple values that we want to log?</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> x = <span class="hljs-number">1</span>
<span class="hljs-keyword">let</span> y = <span class="hljs-number">2</span>
<span class="hljs-keyword">let</span> z = <span class="hljs-number">3</span>
</code></pre>
<p>Instead of typing <code>console.log()</code> three times we can include them all. And we can add a string before each of them if we wanted, too.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> x = <span class="hljs-number">1</span>
<span class="hljs-keyword">let</span> y = <span class="hljs-number">2</span>
<span class="hljs-keyword">let</span> z = <span class="hljs-number">3</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"x:"</span>, x, <span class="hljs-string">"y:"</span>, y, <span class="hljs-string">"z:"</span>, z)
</code></pre>
<p>But that's too much work. Just wrap them with curly braces! Now you get an object with the named values.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> x = <span class="hljs-number">1</span>
<span class="hljs-keyword">let</span> y = <span class="hljs-number">2</span>
<span class="hljs-keyword">let</span> z = <span class="hljs-number">3</span>
<span class="hljs-built_in">console</span>.log( {x, y, z} )
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/image-34.png" alt="Console Output" width="600" height="400" loading="lazy"></p>
<p><em>Console Output</em></p>
<p>You can do the same thing with an object.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> user = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">'Jesse'</span>,
  <span class="hljs-attr">contact</span>: {
    <span class="hljs-attr">email</span>: <span class="hljs-string">'codestackr@gmail.com'</span>
  }
}
<span class="hljs-built_in">console</span>.log(user)
<span class="hljs-built_in">console</span>.log({user})
</code></pre>
<p>The first log will print the properties within the user object. The second will identify the object as "user" and print the properties within it.</p>
<p>If you are logging many things to the console, this can help you to identify each log.</p>
<h2 id="heading-variables-within-the-log">Variables within the log</h2>
<p>Did you know that you can use portions of your log as variables?</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"%s is %d years old."</span>, <span class="hljs-string">"John"</span>, <span class="hljs-number">29</span>)
</code></pre>
<p>In this example, <code>%s</code> refers to a string option included after the initial value. This would refer to "John".</p>
<p>The <code>%d</code> refers to a digit option included after the initial value. This would refer to 29.</p>
<p>The output of this statement would be: "John is 29 years old.".</p>
<h2 id="heading-variations-of-logs">Variations of logs</h2>
<p>There are a few variations of logs. There is the most widely used <code>console.log()</code>. But there is also:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Console Log'</span>)
<span class="hljs-built_in">console</span>.info(<span class="hljs-string">'Console Info'</span>)
<span class="hljs-built_in">console</span>.debug(<span class="hljs-string">'Console Debug'</span>)
<span class="hljs-built_in">console</span>.warn(<span class="hljs-string">'Console Warn'</span>)
<span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Console Error'</span>)
</code></pre>
<p>These variations add styling to our logs in the console. For instance, the <code>warn</code> will be colored yellow, and the <code>error</code> will be colored red.</p>
<p>Note: The styles vary from browser to browser.</p>
<h2 id="heading-optional-logs">Optional Logs</h2>
<p>We can print messages to the console conditionally with <code>console.assert()</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> isItWorking = <span class="hljs-literal">false</span>
<span class="hljs-built_in">console</span>.assert(isItWorking, <span class="hljs-string">"this is the reason why"</span>)
</code></pre>
<p>If the first argument is false, then the message will be logged.</p>
<p>If we were to change <code>isItWorking</code> to <code>true</code>, then the message will not be logged.</p>
<h2 id="heading-counting">Counting</h2>
<p>Did you know that you can count with console?</p>
<pre><code class="lang-js"><span class="hljs-keyword">for</span>(i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10</span>; i++){
  <span class="hljs-built_in">console</span>.count()
}
</code></pre>
<p>Each iteration of this loop will print a count to the console. You will see "default: 1, default: 2", and so on until it reaches 10.</p>
<p>If you run this same loop again you will see that the count picks up where it left off; 11 - 20.</p>
<p>To reset the counter we can use <code>console.countReset()</code>.</p>
<p>And, if you want to name the counter to something other than "default", you can!</p>
<pre><code class="lang-js"><span class="hljs-keyword">for</span>(i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10</span>; i++){
  <span class="hljs-built_in">console</span>.count(<span class="hljs-string">'Counter 1'</span>)
}
<span class="hljs-built_in">console</span>.countReset(<span class="hljs-string">'Counter 1'</span>)
</code></pre>
<p>Now that we have added a label, you will see "Counter 1, Counter 2", and so on.</p>
<p>And to reset this counter, we have to pass the name into <code>countReset</code>. This way you can have several counters running at the same time and only reset specific ones.</p>
<h2 id="heading-track-time">Track Time</h2>
<p>Besides counting, you can also time something like a stopwatch.</p>
<p>To start a timer we can use <code>console.time()</code>. This will not do anything by itself. So, in this example, we will use <code>setTimeout()</code> to emulate code running. Then, within the timeout, we will stop our timer using <code>console.timeEnd()</code>.</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.time()
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.timeEnd()
}, <span class="hljs-number">5000</span>)
</code></pre>
<p>As you would expect, after 5 seconds, we will have a timer end log of 5 seconds.</p>
<p>We can also log the current time of our timer while it's running, without stopping it. We do this by using <code>console.timeLog()</code>.</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.time()

<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.timeEnd()
}, <span class="hljs-number">5000</span>)

<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.timeLog()
}, <span class="hljs-number">2000</span>)
</code></pre>
<p>In this example, we will get our 2 second <code>timeLog</code> first, then our 5 second <code>timeEnd</code>.</p>
<p>Just the same as the counter, we can label timers and have multiple running at the same time.</p>
<h2 id="heading-groups">Groups</h2>
<p>Another thing that you can do with <code>log</code> is group them. ?</p>
<p>We start a group by using <code>console.group()</code>. And we end a group with <code>console.groupEnd()</code>.</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I am not in a group'</span>)

<span class="hljs-built_in">console</span>.group()
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I am in a group'</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I am also in a group'</span>)
<span class="hljs-built_in">console</span>.groupEnd()

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I am not in a group'</span>)
</code></pre>
<p>This group of logs will be collapsible. This makes it easy to identify sets of logs.</p>
<p>By default, the group is not collapsed. You can set it to collapsed by using <code>console.groupCollapsed()</code> in place of <code>console.group()</code>.</p>
<p>Labels can also be passed into the <code>group()</code> to better identify them.</p>
<h2 id="heading-stack-trace">Stack Trace</h2>
<p>You can also do a stack trace with <code>console</code>. Just add it into a function.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">one</span>(<span class="hljs-params"></span>) </span>{
  two()
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">two</span>(<span class="hljs-params"></span>) </span>{
  three()
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">three</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.trace()
}
one()
</code></pre>
<p>In this example, we have very simple functions that just call each other. Then, in the last function, we call <code>console.trace()</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/image-35.png" alt="Console Output" width="600" height="400" loading="lazy"></p>
<p><em>Console Output</em></p>
<h2 id="heading-tables">Tables</h2>
<p>Here's one of the most mind-blowing uses for console: <code>console.table()</code>.</p>
<p>So let's set up some data to log:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> devices = [
  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">'iPhone'</span>,
    <span class="hljs-attr">brand</span>: <span class="hljs-string">'Apple'</span>
  },
  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">'Galaxy'</span>,
    <span class="hljs-attr">brand</span>: <span class="hljs-string">'Samsung'</span>
  }
]
</code></pre>
<p>Now we'll log this data using <code>console.table(devices)</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/image-36.png" alt="Console Output" width="600" height="400" loading="lazy"></p>
<p><em>Console Output</em></p>
<p>But wait – it gets better!</p>
<p>If we only want the brands, just <code>console.table(devices, ['brand'])</code>!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/09/image-37.png" alt="Console Output" width="600" height="400" loading="lazy"></p>
<p><em>Console Output</em></p>
<p>How about a more complex example? In this example, we'll use jsonplaceholder.</p>
<pre><code class="lang-js"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUsers</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> response = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">'https://jsonplaceholder.typicode.com/users'</span>)
  <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> response.json()

  <span class="hljs-built_in">console</span>.table(data, [<span class="hljs-string">'name'</span>, <span class="hljs-string">'email'</span>])
}

getUsers()
</code></pre>
<p>Here we are just printing the "name" and "email". If you <code>console.log</code> all of the data, you will see that there are many more properties for each user.</p>
<h2 id="heading-style">Style ?</h2>
<p>Did you know that you can use CSS properties to style your logs?</p>
<p>To do this, we use <code>%c</code> to specify that we have styles to add. The styles get passed into the second argument of the <code>log</code>.</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"%c This is yellow text on a blue background."</span>, <span class="hljs-string">"color:yellow; background-color:blue"</span>)
</code></pre>
<p>You can use this to make your logs stand out.</p>
<h2 id="heading-clear">Clear</h2>
<p>If you are trying to troubleshoot an issue using logs, you may be refreshing a lot and your console may get cluttered.</p>
<p>Just add <code>console.clear()</code> to the top of your code and you'll have a fresh console every time you refresh. ?</p>
<p>Just don't add it to the bottom of your code, lol.</p>
<h2 id="heading-thanks-for-reading">Thanks for Reading!</h2>
<p>If you want to revisit the concepts in this article via video, you can check out this <a target="_blank" href="https://youtu.be/_-bHhEGcDiQ">video-version I made here</a>.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/_-bHhEGcDiQ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/footer-banner-1.png" alt="Jesse Hall (codeSTACKr)" width="600" height="400" loading="lazy"></p>
<p>I'm Jesse from Texas. Check out my other content and let me know how I can help you on your journey to becoming a web developer.</p>
<ul>
<li><p><a target="_blank" href="https://youtube.com/codeSTACKr">Subscribe To My YouTube</a></p>
</li>
<li><p>Say Hello! <a target="_blank" href="https://instagram.com/codeSTACKr">Instagram</a> | <a target="_blank" href="https://twitter.com/codeSTACKr">Twitter</a></p>
</li>
<li><p><a target="_blank" href="https://codestackr.com/">Sign Up For My Newsletter</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to go beyond console.log and get the most out of your browser’s debugging console ]]>
                </title>
                <description>
                    <![CDATA[ By Gilad Dayagi The console object is a very useful feature of browsers that has been around for many years. It provides access to the browser’s debugging console.Most web developers know how to print messages to the console using console.log. But I’... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-go-beyond-console-log-and-get-the-most-out-of-your-browsers-debugging-console-e185256a1115/</link>
                <guid isPermaLink="false">66c35281bbe01f981047868d</guid>
                
                    <category>
                        <![CDATA[ console ]]>
                    </category>
                
                    <category>
                        <![CDATA[ debugging ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 17 Jul 2018 14:42:52 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*Lvtq6fDOejQmexdK5t2Npw.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Gilad Dayagi</p>
<p>The <code>console</code> object is a very useful feature of browsers that has been around for many years. It provides access to the browser’s debugging console.<br>Most web developers know how to print messages to the console using <code>console.log</code>. But I’ve found that many don’t know about other features of the <code>console</code>, even though they can be very useful for every web developer.</p>
<p>In this post, I’ll go over some of these lesser known features and capabilities. I hope that you will find them useful and interesting, and will incorporate them into your day to day workflow and code.</p>
<p>I added a screenshot of the result of each example. If you want to try things for yourself, just open the DevTools and copy-paste the examples.</p>
<h3 id="heading-using-multiple-arguments">Using multiple arguments</h3>
<p>It is quite common to log several values together. These may be a message along with a related value or the content of several related variables.</p>
<p>Here are two ways I’ve seen developers achieve this:</p>
<h4 id="heading-1-string-concatenation">1. String concatenation</h4>
<pre><code><span class="hljs-keyword">const</span> a = <span class="hljs-number">123</span>;<span class="hljs-keyword">const</span> b = <span class="hljs-string">'abc'</span>;<span class="hljs-keyword">const</span> c = {<span class="hljs-attr">aa</span>: <span class="hljs-number">234</span>, <span class="hljs-attr">bb</span>: <span class="hljs-number">345</span>};<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Foo bar '</span> + a + <span class="hljs-string">' '</span> + b + <span class="hljs-string">' '</span> + c);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/WFSwkoS7zJAs08juQqlWTIZ2O2I8NVELVZ1Y" alt="Image" width="474" height="46" loading="lazy">
<em>Result of string concatenation</em></p>
<h4 id="heading-2-using-multiple-calls">2. Using multiple calls</h4>
<pre><code><span class="hljs-keyword">const</span> a = <span class="hljs-number">123</span>;<span class="hljs-keyword">const</span> b = <span class="hljs-string">'abc'</span>;<span class="hljs-keyword">const</span> c = {<span class="hljs-attr">aa</span>: <span class="hljs-number">234</span>, <span class="hljs-attr">bb</span>: <span class="hljs-number">345</span>};<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Foo bar'</span>);<span class="hljs-built_in">console</span>.log(a);<span class="hljs-built_in">console</span>.log(b);<span class="hljs-built_in">console</span>.log(c);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/d2KZzOM5YcPtJSnoSwxdg4rS9XrUEkAKs6lr" alt="Image" width="354" height="180" loading="lazy">
<em>Result of multiple calls</em></p>
<p>These methods may work (sort of), but:</p>
<ul>
<li>They are not flexible</li>
<li>They are not very readable</li>
<li>They are cumbersome to write</li>
<li>They need special means to work properly with object variables</li>
</ul>
<p>There are several better alternatives for outputting many variables. The most useful one for quick data dump is sending multiple arguments to <code>console.log</code>, like so:</p>
<pre><code><span class="hljs-keyword">const</span> a = <span class="hljs-number">123</span>;<span class="hljs-keyword">const</span> b = <span class="hljs-string">'abc'</span>;<span class="hljs-keyword">const</span> c = {<span class="hljs-attr">aa</span>: <span class="hljs-number">234</span>, <span class="hljs-attr">bb</span>: <span class="hljs-number">345</span>};<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Foo bar'</span>, a, b, c);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/5oOGL4AISGJppBuuhXTHdQst6tNS6rK0a5oO" alt="Image" width="550" height="60" loading="lazy">
<em>Result of multiple arguments</em></p>
<p>This is very handy for debugging, but the output is not very controllable. For output that is intended to be read (like for a library), I would use a different method, which we’ll get to later on.</p>
<h3 id="heading-using-different-log-levels">Using different log levels</h3>
<p>Besides the familiar <code>console.log</code>, there are other logging methods that correspond to different log levels:</p>
<pre><code><span class="hljs-built_in">console</span>.debug(<span class="hljs-string">'Debug message'</span>);<span class="hljs-built_in">console</span>.info(<span class="hljs-string">'Info message'</span>);<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Good old log message'</span>);<span class="hljs-built_in">console</span>.warn(<span class="hljs-string">'A warning message'</span>);<span class="hljs-built_in">console</span>.error(<span class="hljs-string">'This is an error'</span>);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/7LZRb5ZUSjcnw5ObZc96va9fzNjsJpudvrhS" alt="Image" width="488" height="224" loading="lazy">
<em>Log levels as seen in Google Chrome</em></p>
<p>Each log level may have a different default style, which makes spotting errors and warnings at a glance easier.</p>
<p>You can usually also filter which log levels you want to be visible in the DevTools console. This may help reduce clutter.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/4FS42J3jrB8sTwERLqDj-KS-5-8bq8GkXw0T" alt="Image" width="424" height="356" loading="lazy">
<em>Filtering log levels in Google Chrome</em></p>
<p>The appearance of the different levels and the filtering granularity changes from browser to browser.</p>
<h3 id="heading-grouping-console-lines">Grouping console lines</h3>
<p>Some times it is useful to group log messages together. It may allow for more organised and readable output.</p>
<p>This is actually very simple to achieve:</p>
<pre><code><span class="hljs-built_in">console</span>.group();<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'First message'</span>);<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Second message'</span>);<span class="hljs-built_in">console</span>.groupEnd();
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/GYLLEc1rejWitvOzcAAIUyrzWGxYkIe7C4OI" alt="Image" width="426" height="136" loading="lazy">
<em>Grouped log messages</em></p>
<p>Note that log groups can also be nested and labeled:</p>
<pre><code><span class="hljs-built_in">console</span>.group(<span class="hljs-string">'Group aaa'</span>);<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'First message'</span>);<span class="hljs-built_in">console</span>.group(<span class="hljs-string">'Group bbb'</span>);<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'level 2 message a'</span>);<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Level 2 message b'</span>);<span class="hljs-built_in">console</span>.groupEnd();<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Second message'</span>);<span class="hljs-built_in">console</span>.groupEnd();
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/ddmYWHrx7W0lAeHRE0mLPx7wKk-adr2lxT00" alt="Image" width="448" height="266" loading="lazy">
<em>Nested and labeled groups</em></p>
<p>In case you want the group to appear collapsed, use <code>console.groupCollapsed()</code></p>
<h3 id="heading-measuring-performance">Measuring performance</h3>
<p>Measuring the time between points in the code can serve as a quick way to check the performance of some operations.</p>
<p>Here is a trivial way to do this:</p>
<pre><code><span class="hljs-keyword">const</span> start = <span class="hljs-built_in">Date</span>.now();<span class="hljs-comment">// do some stuffconsole.log('Took ' + (Date.now() - start) + ' millis');</span>
</code></pre><p>This works, but there’s a more elegant way to achieve something similar:</p>
<pre><code><span class="hljs-built_in">console</span>.time(<span class="hljs-string">'Label 1'</span>);<span class="hljs-comment">// do some stuffconsole.timeEnd('Label 1');</span>
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/OBuRCdSdW7cLv9BSmKHsTGcpwizTlUehKbEj" alt="Image" width="780" height="146" loading="lazy">
<em>Measuring time with the console</em></p>
<p>The code is shorter, the measurement is more accurate, and you can keep track of up to 10,000 different timers in parallel on a page.</p>
<h3 id="heading-string-substitution">String substitution</h3>
<p>Previously we learned that you can pass multiple arguments to <code>console.log</code> to output multiple values simultaneously. Another way to achieve something similar is to use string substitution. This method requires familiarity with the available placeholders, but offers greater control over the output.</p>
<pre><code><span class="hljs-keyword">const</span> a = <span class="hljs-number">123</span>;<span class="hljs-keyword">const</span> b = <span class="hljs-string">'abc'</span>;<span class="hljs-keyword">const</span> c = {<span class="hljs-attr">aa</span>: <span class="hljs-number">234</span>, <span class="hljs-attr">bb</span>: <span class="hljs-number">345</span>};<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'number %d string %s object %o'</span>, a, b, c);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/m5jIme1eJX9jT0wCHsBxA6mSUyG70r5Af4aQ" alt="Image" width="752" height="58" loading="lazy">
<em>Logging with string substitution</em></p>
<p>Take a look at the documentation (link in the end) for a list of available placeholders.</p>
<h3 id="heading-styling">Styling</h3>
<p>It can be nice to style different log messages differently to increase readability.</p>
<p>We already mentioned that browsers give different default styling to some log levels, but this can also be customized according to your specific needs. Styling is done using a subset of CSS rules, passed in a string as the second parameter, and applied using the marker <code>%c</code>.</p>
<p>Note that you can have different styles for different parts of the log message.</p>
<p>For example:</p>
<pre><code><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Normal %cStyled %clorem %cipsum"</span>, <span class="hljs-string">"color: blue; font-weight: bold"</span>, <span class="hljs-string">"color: red"</span>, <span class="hljs-string">"background-image: linear-gradient(red, blue); color: white; padding: 5px;"</span>);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/al9DXYCJEKx4DtYd1gHx4K8CjKrn0xRCEznU" alt="Image" width="440" height="82" loading="lazy">
<em>Styled log messages</em></p>
<h3 id="heading-summary">Summary</h3>
<p>In this post we have seen some of the features of <code>console</code> that I think are less well-known and more useful. This is by no means an exhaustive list of everything the <code>console</code> can do, as it has many more tricks up its sleeve.</p>
<p>If this got you interested and you want to find out what other things you can do with the <code>console</code>, I recommend reading the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/console">relevant documentation on MDN</a> and trying things out in DevTools.</p>
<p><em>If you found this useful please share this article on social media.</em><br><em>You can also follow me on twitter (@giladaya). Thanks for reading!</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to get the most out of the JavaScript console ]]>
                </title>
                <description>
                    <![CDATA[ By Darryl Pargeter One of the most basic debugging tools in JavaScript is console.log(). The console comes with several other useful methods that can add to a developer’s debugging toolkit. You can use the console to perform some of the following tas... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-get-the-most-out-of-the-javascript-console-b57ca9db3e6d/</link>
                <guid isPermaLink="false">66c3527039769b84d9fe9728</guid>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ console ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 05 Jun 2017 16:29:17 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*mM2AMk0TRENA2zF2RMEebA.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Darryl Pargeter</p>
<p>One of the most basic debugging tools in JavaScript is <code>console.log()</code>. The <code>console</code> comes with several other useful methods that can add to a developer’s debugging toolkit.</p>
<p>You can use the <code>console</code> to perform some of the following tasks:</p>
<ul>
<li>Output a timer to help with simple benchmarking</li>
<li>Output a table to display an array or object in an easy-to-read format</li>
<li>Apply color and other styling options to the output with CSS</li>
</ul>
<h3 id="heading-the-console-object">The Console Object</h3>
<p>The <code>console</code> object gives you access to the browser’s console. It lets you output strings, arrays, and objects that help debug your code. The <code>console</code> is part of the <code>window</code> object, and is supplied by the <a target="_blank" href="https://www.w3schools.com/js/js_window.asp">Browser Object Model (BOM)</a>.</p>
<p>We can get access to the <code>console</code> in one of two ways:</p>
<ol>
<li><code>window.console.log('This works')</code></li>
<li><code>console.log('So does this')</code></li>
</ol>
<p>The second option is basically a reference to the former, so we’ll use the latter to save keystrokes.</p>
<p>One quick note about the BOM: it does not have a set standard, so each browser implements it in slightly different ways. I tested all of my examples in both Chrome and Firefox, but your output may appear differently depending on your browser.</p>
<h3 id="heading-outputting-text">Outputting text</h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/CDHqDu8Ng7T8WrGBf9mgWokjyNnIEjd2ShO0" alt="Image" width="779" height="290" loading="lazy">
<em>Logging text to the console</em></p>
<p>The most common element of the <code>console</code> object is <code>console.log</code>. For most scenarios, you’ll use it to get the job done.</p>
<p>There are four different ways of outputting a message to the console:</p>
<ol>
<li><code>log</code></li>
<li><code>info</code></li>
<li><code>warn</code></li>
<li><code>error</code></li>
</ol>
<p>All four work the same way. All you do is pass one or more arguments to the selected method. It then displays a different icon to indicate its logging level. In the examples below, you can see the difference between an info-level log and a warning/error-level log.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/DgMbvQnFNQOC7Y-1bMrzwtt9GJrXX9XQUAxy" alt="Image" width="783" height="169" loading="lazy">
<em>Simple and easy to read output</em></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/kJBGhn74KnIDM7HH23K6Lj9WS3z14rNLnKNq" alt="Image" width="778" height="173" loading="lazy">
<em>With many things going on this can become hard to read</em></p>
<p>You may have noticed the error log message - it’s more showy than the others. It displays both a red background and a <a target="_blank" href="https://en.wikipedia.org/wiki/Stack_trace">stack trace</a>, where <code>info</code> and <code>warn</code> do not. Though <code>warn</code> does have a yellow background in Chrome.</p>
<p>These visual differences help when you need to identify any errors or warnings in the console at a quick glance. You would want to make sure that they are removed for production-ready apps, unless they are there to warn other developers that they are doing something wrong with your code.</p>
<h3 id="heading-string-substitutions">String Substitutions</h3>
<p>This technique uses a placeholder in a string that is replaced by the other argument(s) you pass to the method. For example:</p>
<p><strong>Input</strong>: <code>console.log('string %s', 'substitutions')</code></p>
<p><strong>Output</strong>: <code>string substitutions</code></p>
<p>The <code>%s</code> is the placeholder for the second argument <code>'substitutions'</code> that comes after the comma. Any strings, integers, or arrays will be converted to a string and will replace the <code>%s</code>. If you pass an object, it will display <code>[object Object]</code>.</p>
<p>If you want to pass an object, you need to use <code>%o</code> or <code>%O</code> instead of <code>%s</code>.</p>
<p><code>console.log('this is an object %o', { obj: { obj2: 'hello' }})</code></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/7e7Q5uEtCa9FDlBIV2bDnMKrgGMQzLj7MuNv" alt="Image" width="535" height="63" loading="lazy"></p>
<h4 id="heading-numbers">Numbers</h4>
<p>String substitution can be used with integers and floating-point values by using:</p>
<ul>
<li><code>%i</code> or <code>%d</code> for integers,</li>
<li><code>%f</code> for floating-points.</li>
</ul>
<p><strong>Input</strong>: <code>console.log('int: %d, floating-point: %f', 1, 1.5)</code></p>
<p><strong>Output</strong>: <code>int: 1, floating-point: 1.500000</code></p>
<p>Floats can be formatted to display only one digit after the decimal point by using <code>%.1f</code>. You can do <code>%.nf</code> to display n amount of digits after the decimal.</p>
<p>If we formatted the above example to display one digit after the decimal point for the floating-point number, it would look like this:</p>
<p><strong>Input</strong>: <code>console.log('int: %d, floating-point: %.1f', 1, 1.5)</code></p>
<p><strong>Output</strong>:<code>int: 1, floating-point: 1.5</code></p>
<h4 id="heading-formatting-specifiers">Formatting specifiers</h4>
<ol>
<li><code>%s</code> | replaces an element with a string</li>
<li><code>%(d|i)</code>| replaces an element with an integer</li>
<li><code>%f</code> | replaces an element with a float</li>
<li><code>%(o|O)</code> | element is displayed as an object.</li>
<li><code>%c</code> | Applies the provided CSS</li>
</ol>
<h4 id="heading-string-templates">String Templates</h4>
<p>With the advent of ES6, template literals are an alternative to substitutions or concatenation. They use backticks (<code>`) instead of quotation marks, and variables go inside</code>${}`:</p>
<pre><code><span class="hljs-keyword">const</span> a = <span class="hljs-string">'substitutions'</span>;
</code></pre><pre><code><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`bear: <span class="hljs-subst">${a}</span>`</span>);
</code></pre><pre><code><span class="hljs-comment">// bear: substitutions</span>
</code></pre><p>Objects display as <code>[object Object]</code> in template literals, so you’ll need to use substitution with <code>%o</code> or <code>%O</code> to see the details, or log it separately by itself.</p>
<p>Using substitutions or templates creates code that’s easier to read compared to using string concatenation: <code>console.log('hello' + str + '!');</code>.</p>
<h4 id="heading-pretty-color-interlude">Pretty color interlude!</h4>
<p>Now it is time for something a bit more fun and colorful!</p>
<p>It is time to make our <code>console</code> pop with different colors with string substitutions.</p>
<p>I will be using a mocked Ajax example that give us both a success (in green) and failure (in red) to display. Here’s the output and code:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/n1PXbwS2SeGH0dXE007ItqJHQCuggJHktM4R" alt="Image" width="800" height="94" loading="lazy">
<em>Successful bears and failing bats</em></p>
<pre><code><span class="hljs-keyword">const</span> success = [ <span class="hljs-string">'background: green'</span>, <span class="hljs-string">'color: white'</span>, <span class="hljs-string">'display: block'</span>, <span class="hljs-string">'text-align: center'</span>].join(<span class="hljs-string">';'</span>);
</code></pre><pre><code><span class="hljs-keyword">const</span> failure = [ <span class="hljs-string">'background: red'</span>, <span class="hljs-string">'color: white'</span>, <span class="hljs-string">'display: block'</span>, <span class="hljs-string">'text-align: center'</span>].join(<span class="hljs-string">';'</span>);
</code></pre><pre><code><span class="hljs-built_in">console</span>.info(<span class="hljs-string">'%c /dancing/bears was Successful!'</span>, success);<span class="hljs-built_in">console</span>.log({<span class="hljs-attr">data</span>: { <span class="hljs-attr">name</span>: <span class="hljs-string">'Bob'</span>, <span class="hljs-attr">age</span>: <span class="hljs-string">'unknown'</span>}}); <span class="hljs-comment">// "mocked" data response</span>
</code></pre><pre><code><span class="hljs-built_in">console</span>.error(<span class="hljs-string">'%c /dancing/bats failed!'</span>, failure);<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'/dancing/bats Does not exist'</span>);
</code></pre><p>You apply CSS rules in the string substitution with the <code>%c</code> placeholder.</p>
<pre><code><span class="hljs-built_in">console</span>.error(<span class="hljs-string">'%c /dancing/bats failed!'</span>, failure);
</code></pre><p>Then place your CSS elements as a string argument and you can have CSS-styled logs. You can add more than one <code>%c</code> into the string as well.</p>
<pre><code><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'%cred %cblue %cwhite'</span>,<span class="hljs-string">'color:red;'</span>,<span class="hljs-string">'color:blue;'</span>, <span class="hljs-string">'color: white;'</span>)
</code></pre><p>This will output the words ‘red’, ‘blue’ and ‘white’ in their respected colors.</p>
<p>There are quite a few CSS properties supported by the console. I would recommend experimenting to see what works and what doesn’t. Again, your results may vary depending on your browser.</p>
<h3 id="heading-other-available-methods">Other available methods</h3>
<p>Here are a few other available <code>console</code> methods. Note that some items below have not had their APIs standardized, so there may be incompatibilities between the browsers. The examples were created with Firefox 51.0.1.</p>
<h4 id="heading-assert">Assert()</h4>
<p><code>Assert</code> takes two arguments — if the first argument evaluates to a falsy value, then it displays the second argument.</p>
<pre><code><span class="hljs-keyword">let</span> isTrue = <span class="hljs-literal">false</span>;
</code></pre><pre><code><span class="hljs-built_in">console</span>.assert(isTrue, <span class="hljs-string">'This will display'</span>);
</code></pre><pre><code>isTrue = <span class="hljs-literal">true</span>;
</code></pre><pre><code><span class="hljs-built_in">console</span>.assert(isTrue, <span class="hljs-string">'This will not'</span>);
</code></pre><p>If the assertion is false, it outputs to the console. It’s displayed as an error-level log as mentioned above, giving you both a red error message and a stack trace.</p>
<h4 id="heading-dir">Dir()</h4>
<p>The <code>dir</code> method displays an interactive list of the object passed to it.</p>
<pre><code><span class="hljs-built_in">console</span>.dir(<span class="hljs-built_in">document</span>.body);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/7H18v4UkQtV4pWkPQRd-2SZ59PAJrCJRs8wU" alt="Image" width="800" height="264" loading="lazy">
<em>Chrome displays dir differently</em></p>
<p>Ultimately, <code>dir</code> only saves one or two clicks. If you need to inspect an object from an API response, then displaying it in this structured way can save you some time.</p>
<h4 id="heading-table">Table()</h4>
<p>The <code>table</code> method displays an array or object as a table.</p>
<pre><code><span class="hljs-built_in">console</span>.table([<span class="hljs-string">'Javascript'</span>, <span class="hljs-string">'PHP'</span>, <span class="hljs-string">'Perl'</span>, <span class="hljs-string">'C++'</span>]);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/AofilibA6MzrmyNl3H7RsgBm1K2QLuyV29EI" alt="Image" width="800" height="99" loading="lazy">
<em>Output for an array</em></p>
<p>The array’s indices or object property names come under the left-hand index column. Then the values are displayed in the right-hand column.</p>
<pre><code><span class="hljs-keyword">const</span> superhero = {     <span class="hljs-attr">firstname</span>: <span class="hljs-string">'Peter'</span>,    <span class="hljs-attr">lastname</span>: <span class="hljs-string">'Parker'</span>,}<span class="hljs-built_in">console</span>.table(superhero);
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/SbvV6TS9682I3VROSpaOYkGwQvAcwxLiNZJ3" alt="Image" width="495" height="70" loading="lazy">
<em>Output for an object</em></p>
<p><strong>Note for Chrome users:</strong> This was brought to my attention by a co-worker but the above examples of the <code>table</code> method don’t seem to work in Chrome. You can work around this issue by placing any items into an array of arrays or an array of objects:</p>
<pre><code><span class="hljs-built_in">console</span>.table([[<span class="hljs-string">'Javascript'</span>, <span class="hljs-string">'PHP'</span>, <span class="hljs-string">'Perl'</span>, <span class="hljs-string">'C++'</span>]]);
</code></pre><pre><code><span class="hljs-keyword">const</span> superhero = {     <span class="hljs-attr">firstname</span>: <span class="hljs-string">'Peter'</span>,    <span class="hljs-attr">lastname</span>: <span class="hljs-string">'Parker'</span>,}<span class="hljs-built_in">console</span>.table([superhero]);
</code></pre><h4 id="heading-group">Group()</h4>
<p><code>console.group()</code> is made up of at least a minimum of three <code>console</code> calls, and is probably the method that requires the most typing to use. But it’s also one of the most useful (especially for developers using <a target="_blank" href="https://github.com/evgenyrodionov/redux-logger">Redux Logger</a>).</p>
<p>A somewhat basic call looks like this:</p>
<pre><code><span class="hljs-built_in">console</span>.group();<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'I will output'</span>);<span class="hljs-built_in">console</span>.group();<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'more indents'</span>)<span class="hljs-built_in">console</span>.groupEnd();<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'ohh look a bear'</span>);<span class="hljs-built_in">console</span>.groupEnd();
</code></pre><p>This will output multiple levels and will display differently depending on your browser.</p>
<p>Firefox shows an indented list:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/jSdMYntev2ydVCx140kpmRe7tEjhFnz8EJXz" alt="Image" width="264" height="133" loading="lazy"></p>
<p>Chrome shows them in the style of an object:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/Oh8RGjXDlufRuWiz0FWDsfRN7aTIxWcqaio3" alt="Image" width="176" height="116" loading="lazy"></p>
<p>Each call to <code>console.group()</code> will start a new group, or create a new level if it’s called inside a group. Each time you call <code>console.groupEnd()</code> it will end the current group or level and move back up one level.</p>
<p>I find the Chrome output style is easier to read since it looks more like a collapsible object.</p>
<p>You can pass <code>group</code> a header argument that will be displayed over <code>console.group</code>:</p>
<pre><code><span class="hljs-built_in">console</span>.group(<span class="hljs-string">'Header'</span>);
</code></pre><p>You can display the group as collapsed from the outset if you call <code>console.groupCollapsed()</code>. Based on my experience, this seems to work only in Chrome.</p>
<h4 id="heading-time">Time()</h4>
<p>The <code>time</code> method, like the <code>group</code> method above, comes in two parts.</p>
<p>A method to start the timer and a method to end it.</p>
<p>Once the timer has finished, it will output the total runtime in milliseconds.</p>
<p>To start the timer, you use <code>console.time('id for timer')</code> and to end the timer you use <code>console.timeEnd('id for timer')</code> . You can have up to 10,000 timers running simultaneously.</p>
<p>The output will look a bit like this <code>timer: 0.57ms</code></p>
<p>It is very useful when you need to do a quick bit of benchmarking.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>There we have it, a bit of a deeper look into the console object and some of the other methods that come with it. These methods are great tools to have available when you need to debug code.</p>
<p>There are a couple of methods that I have not talked about as their API is still changing. You can read more about them or the console in general on the <a target="_blank" href="https://developer.mozilla.org/en/docs/Web/API/console">MDN Web API page</a> and its <a target="_blank" href="https://console.spec.whatwg.org/">living spec page</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/JpR7GD0Z7rB6uiRKYUTQ9zJGSsQrXVZDCjjW" alt="Image" width="256" height="224" loading="lazy">
_[https://developer.mozilla.org/en/docs/Web/API/console](https://developer.mozilla.org/en/docs/Web/API/console" rel="noopener" target="<em>blank" title=")</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
