In case you wonder what the hell
Many developers use the
console.log in one particular way to log information, they piece together the message they want to log. Mostly this looks somehow similar to the following example.
var playerCount = 11, userName = 'David'; // magic stuff console.log('Game statistics: ' + playerCount + ' players online. Logged in as: ' + userName); // output: Game statistics: 11 players online. Logged in as: David
This style is perfectly O.K., but not the best we can get because all these
' make it hard to quickly change the message without breaking it. And in my opinion this style is also confusing and ugly.
In order to make it more convenient we can use string substitution patterns in the console.log method. Developers familiar with
The first argument of
console.log is the message we want to log. We can put placeholders into this string which will get replaced in the output by the values of the additional arguments. There are 5 types of placeholders.
%sfor a String value
%ifor a Integer value
%ffor a Floating point number
%ofor an Object hyperlink
console.log will replace the first placeholder in the message string with the first additional argument, the second placeholder with the second additional argument and so forth. The code example from above would look like this if we use placeholders.
var playerCount = 11, userName = 'David'; // magic stuff console.log('Game statistics: %i players online. Logged in as: %s', playerCount, userName); // output: Game statistics: 11 players online. Logged in as: David
« View all posts