According to the documentation provided for console.log
in Node.js, the function follows a printf-like format:
The initial argument is a string that may contain placeholders.
Each placeholder will be substituted with the respective value from its corresponding argument. The supported placeholders include:
%s - String.
%d - Number (both integer and float).
%j - JSON. If circular references are found, it will be replaced by '[Circular]'.
%% - single percent sign ('%'). This placeholder does not require an argument.
In case a placeholder lacks a respective argument, it will remain unchanged.
Hence, whenever %%
appears in a string displayed via console.log
in Node.js (excluding browsers), it will be swapped out for a single %
. Any %s
, %d
, or %j
will be exchanged for a string, number, or JSON string, respectively. Below are some examples:
console.log("This is a string: %s", "Hello, World!");
//= This is a string: Hello, World!
console.log("This is a number: %d", 3.14);
//= This is a number: 3.14
console.log("This is a JSON string: %j", { value: true });
//= This is a JSON string: {"value":true}
console.log("This is a single percentage sign: %%");
//= This is a single percentage sign: %
console.log("If we use %%s and %%d here we get '%s' and '%d'.", "a string", 100);
//= If we use %s and %d here we get 'a string' and '100'.
Using console.log
in a browser environment will simply display the plain string without any substitution as mentioned above.