JSFiddle - React, Tailwind, and code Playground

by Josh Pullen

JavaScript

const myName = "Khang"
const myCity = "Ho Chi Minh City"

// You wrote this, which doesn't work:
// console.log("My name is ." + ${myName} "My favorite city is " + ${myCity}")

// There are two methods of inserting variables into strings, but
// you've combined both methods into one, and that doesn't work.

// My personal favorite method is template literals, which use backticks ``
// and the ${myVariable} syntax. You can learn more about them here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

// This is your code using that method:
console.log(`My name is ${myName}. My favorite city is ${myCity}.`)

// The other method, which is not quite as good, is to use regular strings
// with quotes "" and then to "add" the variable values in using +
// It looks like this:
console.log("My name is " + myName + ". My favorite city is " + myCity + ".")

// Both methods work, but the first option is probably better
// because it's less likely to break when the variable values change.