Monday, 30 November 2020

Be an expert with console.log()

`console.log()` is a most prominent method among developers. It is mainly used for debugging as it prints the output in the console.  But there much more methods....

Here, console is an object which provides access to the browser's debugging console. To open the browser's debugging console, right click on the page, select Inspect and click console. 

The word console is inspired by the word consolidare(to unite/to combine) , from Latin, the architectural  term given to the corbel placed on the end, i.e. height is greater than the projection. 


Corbel Picture



However, the word is more familiar with it's connection to the furniture. The console-table was originally so called because the slab was supported upon a scroll-shaped bracket, or upon legs which in form and contour answered roughly to the idea of a bracket.


Console table image


The Most Common Console Methods: 

  

#Console.log()

 'To log'  means 'to write down'. 

It is used to display general message/output. 


console.log Example

 

# Console.info()

Outputs an informational message to the Web Console.


console.info example


# Console.warn()

Outputs a warning message to user that might cause a problem in future.


console.warn Example


# Console.error()

Outputs an error message. 


console.error example


String Substitution

When passing a string to one of the console object’s methods that accept a string (such as log()), you may use these substitution strings:


`%s `– string                                                                     

`%i` or `%d` – integer                                                             

`%o` or `%0` – object                                                               

`%f`– float                                                                               


string substitution example


# Console.assert()

Outputs an error message to console if assertion is false. If assertion is true, nothing happens.

Syntax : 

console.assert( expression, message )


console.assert Example

# Console.count()

Logs the number of times that this particular call to `count()` has been called.

Syntax : console.count(label) / console.count()

if a label is applied, it has been called with that label.

if a label is omitted, `count()` behaves as though it was called with the "default" label.


console.count Example


# Console.dir()

Displays an interactive list of the properties of the specified JavaScript object. 

`log` only outputs a `toString` representation, whereas `dir` outputs a navigable tree.


console.dir Example


# Console.group() and Console.groupEnd()

Creates new inline group.

You can use nested group to organize output by associating related messages. To move out of the group, use `groupEnd()`

Label is optional. 


Without Label


console.group and console.groupEnd Example

With Label




# Console.table()

Displays tabular data as a table. 

This method takes one mandatory argument `data`, which must be an array or an object. The second parameter `columns` is optional. 

Each element in an array (or enumerable property if data is an object) will be a row in table. 

First column in table will be labeled as `index.` If data is an array, it's value will be the array indices. If data is an object, then its values will be the property names. 


console.table Example

console.table Example

Restricting the column displayed. 

By default, console.table() lists all elements in row. You can use the optional  parameter - `columns`, to select a subset of columns to display:

console.table Example


# Console.time() and Console.timeEnd()

`Console.Time()` - Starts a timer. Use the *label * parameter to name the timer, if you want multiple timers on same page.

It is used for time certain operations in code for testing purposes.

Up to 10,000 simultaneous timers can run on a given page.

`Console.timeEnd()` – Stops the specified timer and outputs elapsed time in  milliseconds, since it was started.

console.time and console.timeEnd Example

Using the label parameter:


console.time and console.timeEnd Example

# Console.trace()

Displays a trace that show how the code ended up at a certain point. (Stack trace)


console.trace Example



# Console.memory

The memory property can be used to check out the heap size status. 


Heap Size :

Amount of memory allocated to objects that are being defined in Apex code. 

Note : memory is property, not method. 



console.memory example





 



















No comments:

Post a Comment