-1

So, I was exercising javascript then I got in doubt on something:

I need to print: O usuário mora em São Paulo / SP, no bairro Centro, na rua "Rua dos Pinheiros" com nº 1293.

But I dont know how to put print endereco.rua with double quotes like the final phrase should be.

<html>
    <head>
        <meta charset="UTF-8">
        <title>Curso Javascript - Rocketseat</title>
    </head>
    <body>
        <script>
            var endereco = {
                rua: 'Rua dos Pinheiros',
                numero: 1293,
                bairro: 'Centro',
                cidade: 'São Paulo',
                uf: "SP"
            }

            console.log('O usuário mora em ' + endereco.cidade + ", / " + endereco.uf + ", na rua " + endereco.rua + " com nº " + endereco.numero + ".")
        </script>
    </body>
</html>
Vitor
  • 5
  • 1

2 Answers2

0

Either escape the double quotes:

console.log('O usuário mora em ' + endereco.cidade + ", / " + endereco.uf + ", na rua \"" + endereco.rua + "\" com nº " + endereco.numero + ".")

or use single quotes around the strings that contain double quotes:

console.log('O usuário mora em ' + endereco.cidade + ", / " + endereco.uf + ', na rua "' + endereco.rua + '" com nº ' + endereco.numero + ".")

And if you're using a modern browser, you can use ES6 template strings to avoid all the concatenation.'

console.log(`O usuário mora em ${endereco.cidade}, / ${endereco.uf}, na rua "${endereco.rua}" com nº ${endereco.numero}.`)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You would use an escape character. Since the string beffore endereco.rua and after are both in double quotes, you'd place \" just before the closing quote before the variable and \" just inside the next string.

Such as this:

console.log('O usuário mora em ' + endereco.cidade + ", / " + endereco.uf + ", na rua \"" + endereco.rua + "\" com nº " + endereco.numero + ".")