Different when we use template literals instead of string concatenation

Is there anything different when we use template literals instead of string concatenation

I don’t understand why we use template literals here

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
console.log(${property}: ${object[property]});
}

Both approaches can produce the same result.

`${expression}`

Within a back tick delimited character string, the templates permit direct interpolation of variables and/or expressions.

1 Like

so can we write like this

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
console.log( {property}: ${object[property]} );
}

Only if the back ticks are present.

console.log(`${property}: ${object[property]}`);

I 'm so sorry I misstyped
I mean this
const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
console.log( property : object[property] );
}
So why we need a template literals if there is no literals
Am I missunderstood something

        templates
        /        \
`${property}: ${object[property]}`
            --
            /
  string literal characters

The templates permit direct interpolation of the expressions property and object[property]

1 Like