4 ms·
another mistake javascript developers make: using %s when it's unnecessary. by way of example, the author says: console.log('delay is %s', chalk.green(delay));
by ixtli 12y ago
another mistake javascript developers make: using %s when it's unnecessary. by way of example, the author says:
console.log('delay is %s', chalk.green(delay));
where as it's much clearer to say
console.log('delay is', chalk.green(delay));
This works in node as of at least v0.10.33 and chrome, but probably most modern browsers.
- nailer 12y agoIf anyone else is wondering: https://www.npmjs.com/package/chalk https://www.npmjs.com/package/chalk
- jbnicolai 12y agoOf which the next version is nearly coming out! (see: https://github.com/sindresorhus/chalk/issues/44 https://github.com/sindresorhus/chalk/issues/44 ). Shameless plug (co-author here), but if you have any questions about it: feel free to ask.
- olalonde 12y agoI personally prefer console.log('delay is ' + chalk.green(delay))); as it is more consistent with non console.log expressions. For example, just with copy/paste I could replace the above with: var msg = 'delay is ' + chalk.green(delay); console.log(msg); But it's really just a matter of preference as long as only one convention is used throughout the same codebase.
- whichdan 12y agoUsing commas is a little bit nicer if you tend to log objects frequently.
- Arnavion 12y agoES6 template strings give you the best of both worlds. console.log(`delay is ${ chalk.green(delay) }`);
- deleted 12y ago[deleted]
- konstruktor 12y agoThis expression is always evaluated, creating a new string object on the heap that has to be collected, whereas the format string one is only evaluated on demand (matching log level). That's why most logging frameworks go for the latter. I haven't seen benchmarks regarding the performance impact, though.
- nawitus 12y agoI don't use console.log when I can. It's better to roll your own logging function which adds timestamps and automatically stringifies the object (preferably using a stringify function which handles cyclic objects). The prolem with using console.log(object) is that in Node.js the object is not stringified and on browsers the debugger displays the current state of the object, not the state when it was printed. I found that out the hard way.. And when you're using your own logging function you can use a proper logging framework like winston to save the logs to a database and a file in addition to the console (we also send client-side logs to the backend using socket.io for easy debugging).
- EpicEng 12y agoExcept you forgot the space. I prefer the format string.