Brincar com strings muitas vezes pode ser algo um pouco trabalhoso (pode-se ler chato também இ_இ). Isso se deve ao fato principalmente de termos que concatenar palavras/frases com variáveis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let author = 'Tiririca'; | |
let word = 'Worse'; | |
let oldMessage = word + ' than it is, it is impossible. - ' + author; | |
console.log(oldMessage); | |
// Worse than it is, it is impossible. - Tiririca |
Template strings para a nooooooooossa alegria.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let author = 'Tiririca'; | |
let word = 'Worse'; | |
let message = `${word} than it is, it is impossible. - ${author}`; | |
console.log(message); | |
// Worse than it is, it is impossible. - Tiririca |
Meu amigo Rafael Rinaldi fez uma boa observacão: é possível utilizar qualquer tipo de expressão e não só variáveis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = { | |
foo: 'bar' | |
}; | |
const fn = foo => foo; | |
console.log( `${obj.foo}` ); | |
// bar | |
console.log( `${fn('aloha')}` ); | |
// aloha |
Aqui você encontra um JS Bin com os exemplos acima.