Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is more Canonical javascript, console.log

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.

like image 475
ThePrimeagen Avatar asked Jun 08 '20 00:06

ThePrimeagen


1 Answers

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)
like image 187
Zze Avatar answered Sep 29 '22 14:09

Zze