Search the Blog

Tuesday, August 13, 2019

JavaScript Counting - console.count()

console.count([obj]) places a counter on the object's value provided as argument. Each time this method is invoked, the counter is increased.
Note:-exception of the empty string 
 A label together with a number is displayed in the debugging console according to the following format:
[label]: X
label represents the value of the object passed as argument and X represents the counter's value.

An object's value is always considered, even if variables are provided as arguments:
var dizsweb_1 = 1, dizsweb_2 = '2', dizsweb_3 = "";
 console.count(dizsweb_1 );
console.count(dizsweb_2 );
 console.count(dizsweb_3 );
console.count(1);
console.count('2');
console.count('');
Displays:
1: 1 2: 1 : 1 1: 2 2: 2 : 1


Strings with numbers are converted to Number objects let see:
console.count(42.3);
console.count(Number('42.3')); console.count('42.3');
Displays:
42.3: 1 42.3: 2 42.3: 3

Functions point always to be  the global Function object:
console.count(console.constructor);
console.count(function(){});
console.count(Object);
var function_1 = function myfn(){};
 console.count(function_1);
console.count(Number);
Displays:
[object Function]: 1
[object Function]: 2
[object Function]: 3
[object Function]: 4
[object Function]: 5


Certain objects get speciļ¬c counters associated to the type of object they refer to:
console.count(undefined);
console.count(document.Batman);
 var obj; console.count(obj);
console.count(Number(undefined));
console.count(NaN);
console.count(NaN+3);
console.count(1/0);
console.count(String(1/0));
 console.count(window);
 console.count(document);
console.count(console);
console.count(console.__proto__);
console.count(console.constructor.prototype); console.count(console.__proto__.constructor.prototype); console.count(Object.getPrototypeOf(console));
console.count(null);
Displays:
undefined: 1
undefined: 2
undefined: 3
NaN: 1
NaN: 2
NaN: 3
Infinity: 1
Infinity: 2
[object Window]: 1
[object HTMLDocument]: 1
[object Object]: 1
[object Object]: 2
[object Object]: 3
[object Object]: 4
[object Object]: 5
null: 1
Empty string or absence of argument

If no argument is provided while sequentially inputting the count method in the debugging console, an empty string is assumed as parameter, i.e.:

> console.count(); 
    : 1
> console.count(''); 
    : 2
> console.count(""); 
    : 3



See also Following links-
JavaScript- Measuring console Time()
Javascript Console opening in different browser

JavaScript : Tabulating values - console.table() method

console.table() can be used to display objects and arrays in a tabular format.

var personArr = [
                                   {        "personId": 101,     
                                             "name": "Sandeep",     
                                             "city": "Titerwara",     
                                             "phoneNo": "1234567890"
                                   },
                                   {        "personId": 102,
                                             "name": "Shiv",
                                             "city": "Buchcha Kheri",   
                                             "phoneNo": "1234567890"
                                   },
                                   {        "personId": 103,     
                                             "name": "Kandela",     
                                             "city": "Perth",     
                                             "phoneNo": "1234567890"
                                   },
                                   {        "personId": 104,     
                                             "name": "Amit",     
                                             "city": "Buchcha Kheri",     
                                             "phoneNo": "1234567890"
                                    }
                           ];

 console.table(personArr, ['name', 'personId']);

Output Will be like This-





JavaScript Stack trace logging console trace method

Including a stack trace when logging console.trace() method of JavaScript

function foo() 
            {  
                 console.trace('Programming Logic and Code'); 
            }
foo();
Will display this in the console:
Programming Logic and Code      VM696:1  foo       
                                                  @ VM696:1  (anonymous function) 
                                                  @ (program):1
Note: Where available it's also useful to know that the same stack trace is accessible as a property of the Error object. This can be useful for post-processing and gathering automated feedback.
                    var e = new Error('foo'); console.log(e.stack);


JavaScript Printing to browser Console for debugging

JavaScript Formatting Console Output


Translate