6 powerful JavaScript console methods you may not know about

warn method

console.warn()Method used to display warning in browser console It takes one parameter (warning text)

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

Insert image description here

error method

console.error()Method allows us to print error messages in the console. It takes the error message as a parameter.
This method is often used for testing purposes.

console.error("This is a error message");

Insert image description here

table method

console.table()Method allows us to easily print objects and arrays in the form of tables within the console

console.table({
    
    Name:"Alex", Age: 25});

Insert image description here

assert method

console.assert()Method allows us to print messages to the console based on conditions. It takes two parameters: if the first evaluates to false, the second parameter is printed.

console.assert(document.getElementById("btn"), "There is no element with ID btn");

Insert image description here

dir method

console.dir()Methods are used with objects. It identifies objects and prints them as an expandable list in the console

const person = {
    
    name: 'John', age: 17, friends: ['Zulie', 'James', 'Alex']};
console.dir(person);

Insert image description here

count and countReset methods

console.count()Method allows counting the number of times count() has been called. The method is calculating itself

for (let i = 0; i <= 6; i++){
    
    
 console.count();
}

If I call it again console.count(), it returns the default value: 8 and so on
. On the other hand, we console.countReset()reset the method counter to 0.
In our case the counter is now the number 8 and if we call console.countReset()the method it will set the counter to 0
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45959525/article/details/119908346
Recommended