I have recently started using typescript and reviewing some of my favorite repos, I have seen people use back ticks for template string in console.log
. This seems odd to me, but I am pretty inexperienced overall with canonical javascript. To me, console.log
already provides the mechanism for formatting by putting in spaces betwixt comments. Why would you use a template string? Is the performance better?
Example:
// set list of items
function printMyItems(item1, item2) {
// What I have been doing
console.log(item1, item2);
// What I have seen
console.log(`${item1} ${item2}`);
}
// Also variadic arguments seem to work better with the former approach.
function printMyItems(...args) {
// What I have been doing
console.log(...args);
// Should I do this instead?
console.log(`${...args}`);
}
Thank you for your time and effort reading this post. I hope I have made myself clear.
There is actually a distinct difference between the 2 implementations when it comes to objects:
var test = {a: 1};
console.log(`${test}`);
test.a = 99;
console.log(`${test}`);
var test2 = {a: 1};
console.log(test2);
test2.a = 99;
console.log(test2);
var test1 = {a: 1};
var test2 = {b: 99};
console.log(`${test1}` == `${test2}`)
console.log(test1 == test2)
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