Can someone please explain what's happening here? I see %d
and %s
but I don't see these declared or written anywhere else in the code. What the heck does this mean/ do in javascript? I'm assuming it's a sort of string templating that I've never seen before?
passport.deserializeUser(
(id, done) => {
debug('will deserialize user.id=%d', id)
User.findById(id)
.then(user => {
debug('deserialize did ok user.id=%d', user.id)
done(null, user)
})
.catch(err => {
debug('deserialize did fail err=%s', err)
done(err)
})
}
)
%d or %i for Number. %f for Floating points. %o for an Object. %j for an JSON.
JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values (UTF-16 code units). Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on.
Creating and Viewing the Output of Strings In JavaScript, there are three ways to write a string — they can be written inside single quotes ( ' ' ), double quotes ( " " ), or backticks ( ` ` ).
We call these text values strings in JavaScript; think of them as a string of letters. To create a string, we surround text in quotation marks: "Hello World!"
What you are seeing there is the string substitution patterns that are built into console.log()
or console.debug()
.
The pattern goes as I have presented below:
%s
for a String
%d
or %i
for Number
%f
for Floating points
%o
for an Object
%j
for an JSON
So essentially you are replacing the specifier with the values supplied as so:
var name = 'Chris';
console.log('Hi, my name is %s.', name);
// Hi, my name is Chris.
console.debug('Hi, my name is %s.', name);
// Hi, my name is Chris.
Docs:
console.log()
and console.debug()
use printf-style formatting. Below are the officially supported formatters:
Formatter representation:
%O
Pretty-print an Object on multiple lines.%o
Pretty-print an Object all on a single line.%s
String.%d
Number (both integer and float).%j
JSON. Replaced with the string '[Circular]' if the argument contains circular references.%%
Single percent sign ('%'). This does not consume an argument.The results are written in the debug console. just open your command-line or terminal and run it using this:
node debug [script.js | -e "script" | <host>:<port>] command
%c
: You can give a style to log text with CSS .
console.log("%c YourText", "color:blue; font-weight:bold;");
You can create multiple style for different console log text.
console.log("%c Text1 %c Text2 %c Text3", "color:red;", "color:green;", "color:blue;");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With