-3

I have seen ${ } being used in many javascript codes. What exactly does this do?

For example:

 updateDisplay(){
        if(this.operation != null){
            this.previousOperandTextElement.innerText = ${this.previousOperand} ${this.operation};
        }
        this.currentOperandTextElement.innerText = this.currentOperand;
    }

Why would we not use + to concatenate in this case?

chibiw3n
  • 173
  • 10

1 Answers1

1

It's called a template literal. It achieves the same thing as concatenation but in a more readable manner:

const a = "Hello"
const b = "."

console.log(`${a} World${b}`)
Richie Bendall
  • 3,783
  • 2
  • 24
  • 36
  • Are there any differences between using template literals and using '+' to combine the strings? Is there any case where it would be preferable to use one over the other? – chibiw3n Aug 12 '20 at 02:30
  • Using templates is vastly more readable that string concatenation. – Adrian Brand Aug 12 '20 at 02:31