Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: %s or %d represents string?

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)
      })
  }
)
like image 762
Turnipdabeets Avatar asked Feb 23 '17 02:02

Turnipdabeets


People also ask

What is %d in js?

%d or %i for Number. %f for Floating points. %o for an Object. %j for an JSON.

What is string format in JavaScript?

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.

How do you display a string in JavaScript?

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 ( ` ` ).

What surrounds a string in JavaScript?

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!"


3 Answers

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:

  • Docs for Chrome
  • Docs for Firefox
  • Docs for IE
  • Docs for Node.js
  • Docs for Spec
like image 89
Chris Cruz Avatar answered Oct 18 '22 02:10

Chris Cruz


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
like image 6
Teocci Avatar answered Oct 18 '22 02:10

Teocci


%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;");
like image 4
maslancan Avatar answered Oct 18 '22 02:10

maslancan