Search the Blog

Monday, August 12, 2019

JavaScript Printing to browser Console for debugging

A browser's debugging console can be used in order to print simple messages. This debugging or web console can be directly opened in the browser  and the log method of the console JavaScript object can be invoked by typing the following:

console.log('Programming Logic And Code');

Press Enter

Output- Programming Logic And Code

var obj = { test: 1 };
console.log(['string'], 1, obj, window);

Output-
console.log({ key1: 'val', key2: ['one', 'two'], key3: { a: 1, b: 2 } });
Output:
Object { key1: 'val', key2: Array[2], key3: Object }



console.log(new Date(0)); 
console.log(function test(a, b) { return c; });

Output-
Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
VM53:1 ƒ test(a, b) { return c; }


Other print method to console-

1- console.info – small informative icon (ⓘ) appears on the left side of the printed string(s) or         object(s).
2- console.warn – small warning icon (!) appears on the left side. In some browsers, the                     background of the log is yellow.
3- console.error – small times icon (⊗) appears on the left side. In some browsers, the                       background of the log is red.
4- console.timeStamp – outputs the current time and a specified string, but is non-standard:
    console.timeStamp('msg');
    Output:
    00:00:00.001 msg
5- console.trace – outputs the current stack trace or displays the same output as the log method       if invoked in the global scope.








See Also
Palindrome JAVA Code- Programming Logic and Code with Syntax
JAVA Pattern Program Code with Pattern 6 Programming Logic and Code with Syntax
JAVA Pattern Program Code with Pattern 5 Programming Logic and Code with Syntax
JAVA Pattern Program Code with Pattern 4 Programming Logic and Code with Syntax
JAVA Pattern Program Code with Pattern 3 Programming Logic and Code with Syntax
JAVA Pattern Program Code with Pattern 2 Programming Logic and Code with Syntax
JAVA Pattern Program Code with Pattern Programming Logic and Code with Syntax
SQL Query for tracking the details of particular transaction in log table Programming Logic and Code with Syntax
SQL Query to track Invoice with multiple join full code Programming Logic and Code with Syntax
SQL Query to make GST Report HSN Code Wise Programming Logic and Code with Syntax
SQL Query for checking the mismatch of common data in two tables
SQL Query to track the details of any item
SQL SUM FUNCTION WITH QUERY AND SYNTAX
SQL AVG FUNCTION WITH QUERY AND SYNTAX
SQL COUNT FUNCTION WITH QUERY AND SYNTAX Programming Logic and Code with Syntax
SQL Order By with Query and Syntex Programming Logic and Code with Syntax
SQL NOT Operator with Syntax and Query Programming Logic and Code with Syntax
SQL OR Operator with Syntax and Query Programming Logic and Code with Syntax
SQL AND Operator with Syntax and Query
SQL Operators
SQL WHERE Clause syntax and Query
SQL Query Pending Purchase Order Details
SQL QUERY Pending dispatch instruction
SQL Query for Ledger Master Register
COMPLETE SQL QUERY FOR LEAVE REGISTER WITH UNION LEFT
SQL SELF JOINS WITH QUERY SYNTAX
SQL Full Joins WITH QUERY AND SYNTAX
SQL Right outer Joins WITH Query WITH SYNTAX
SQL Left Outer Joins with Query
SQL Inner Joins with query
SQL Joins syntax code and query
SQL QUERY Item Wise Final Purchase Details with joins and group by and left outer and order by clause
SQL Query for Item Master with group by and order by and joins
SQL Query to Get The List of All Employees with multiple tables
SQL Debtors Balance Details Query MIS with joins group
QUERY FOR Customer Ageing For Bank with round, convert, joins
SQL Query Creditors Balance Details with inner joins and group by
SQL Distinct keyword with multiple columns
SQL DISTINCT Keyword
Finds any values that start with alphabet and ends with alphabet
SQL Finds any values that start and end with "alphabet"
SQL Finds any values that end with "alphabet" and are at least 7 characters in length
SQL Finds any values that start with "alphabet" and are at least 3 characters in length
SQL Finds any values that start with "alphabet" and are at least 2 characters in length
SQL Search a alphabet or string in a column from end
SQL Search a alphabet or string in a column from Start
SQL Search a alphabet or string in a column with specific position from Start
SQL Like operator
SQL Delete Query
SQL Update Query
Insert Query in SQL Server
SQL Insert Query Without Index

JavaScript Formatting Console Output

In javascript their are various method exists for various data types like in other programming language like C, C++, JAVA etc.


Code and Syntax-
console.log('%s have  Rs%d in my pocket','i',100);

 Output- I have Rs100 in my pocket

Specifier                                                                          Output
%s                                           Used for formats the value as a string
%i or %d                                 Used for formats the value as an integer
%f                                            Used for formats the value as a floating point value
%o                                           Used for formats the value as an expandable DOM element
%O                                          Used for formats the value as an expandable JavaScript object
%c                                           Used for Applies CSS style rules to the output string as specified by                                                      the second paramete


Advanced styling
When the CSS format specifier (%c) is placed at the left side of the string, the print method will accept a second parameter with CSS rules which allow fine-grained control over the formatting of that string:


For multiple %c
console.log("%cHello %cWorld%c!!", // string to be printed           
"color: blue;", // applies color formatting to the 1st substring           
"font-size: xx-large;", // applies font formatting to the 2nd substring           
"/* no CSS rule*/" //
does not apply any rule to the remaining substring );

Sunday, August 11, 2019

JavaScript- Measuring console Time()

With the Help of console function console.Time() we can measure how long a task in your code takes to run.

Program 1-
               //For starting the Time console

          console.time('programming Logic And Code');

            // for  processing 
          alert('Click to continue'); 

              // for measuring the time and closing
          console.timeEnd('programming Logic And Code');

               // for  processing
                alert('One more time'); 
          
                // for measuring the time and closing
           console.timeEnd('programming Logic And Code');


Output 
   1- 22.23 ms
   2- 34.56 ms

Program 2
var     element_programming_logic_code = document.getElementsByTagName('*'); 
//select all elements on the page

console.time('Loop_time_programming_logic_and_code');
         for (var i = 0; i < 5000; i++) 
              {    
                     for (var j = 0, length = element_programming_logic_code .length; j < length; j++) 
                         {       
                               // nothing to do ...   
                         } 
               }
console.timeEnd('Loop_time_programming_logic_and_code');


Output:- 1.324 sec

Translate