Summary - Console methods
Print a dataset neatly using console.group and console.table.
Summary - Console methods
The console methods you learned in this group serve different needs. console.log is for general output, warn and error for warnings and errors, table for a table view, group for organized groups, and time for measuring duration.
Using the right method makes output more readable and speeds up debugging. Now you will use them together by grouping a dataset and showing it as a table.
Exercise Group and table the products
Define an array called products. Each item should be an object with name and price properties (at least two items). Then open a group with console.group("Products"), call console.table(products) inside it, and close the group with console.groupEnd().
Show example solution
const products = [
{ name: "Pen", price: 25 },
{ name: "Notebook", price: 40 }
];
console.group("Products");
console.table(products);
console.groupEnd();
Exercise Measure and print a total
Start a timer with console.time("total"). Initialize an accumulator with let total = 0. Loop with for (let i = 1; i <= 100; i++) and add each i to total. Then print the duration with console.timeEnd("total") and print the result with console.log("Total:", total).
Show example solution
console.time("total");
let total = 0;
for (let i = 1; i <= 100; i++) {
total += i;
}
console.timeEnd("total");
console.log("Total:", total);
Test yourself
-
Match the method to its purpose
- console.log
- General output
- console.warn
- Warning message
- console.error
- Error message
- console.table
- Array/Object table
- console.group
- Grouping outputs
-
Stop the timer
console.time("sorgu"); // sorgu çalışıyor console.____("sorgu");Show answer
console.time("sorgu"); // sorgu çalışıyor console.timeEnd("sorgu");