Search the Blog

Monday, August 12, 2019

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 );

No comments:

Post a Comment

Translate