1

I am brand new to typescript/javascript/angular, and I am reading some tutorials I keep coming across the following type of thing:

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  toString() {
    return `(${this.x}, ${this.y})`;
  }
}

Now my question is these are physical back-ticks and everything else I have seen just uses " and ' which I believe are functionally the same so are these two true:

  1. ` == ' ?

  2. ` === '?

Or is this an Angular/Typescript only thing?

Ivan
  • 34,531
  • 8
  • 55
  • 100
Vladimir_314159
  • 1,457
  • 1
  • 9
  • 21

1 Answers1

2

Template literals are enclosed by the back-tick (`) character instead of double " or single ' quotes. They can contain placeholders, indicated by the dollar sign and curly braces (${expression}). The expressions in the placeholders and the text between them get passed to a function. The default function just concatenates the parts into a single string.

- Source: MDN web docs

You can use black ticks ` to insert JavaScript notations inside your string. For instance:

const name = 'world'

// using ''
let myString1 = 'Hello' + name;

// using ``
let myString2 = `Hello ${name}`

myString1 and myString2 both have both the same string.

This is a more convenient way of formatting content in JavaScript, no need to concatenate strings, you can insert variables inside strings.

And yes ` is equal to '. Try typing the following in the console:

`\`` === '`'

It will return as true

Community
  • 1
  • 1
Ivan
  • 34,531
  • 8
  • 55
  • 100