1

I've trying to use the EcmaScript 6 feature of adding variables in quotes using:

'Sample Text ${variable}'

On the following code:

Cat.prototype.info = function(){
    console.log('Name: ${this.name}');
    console.log('Gender: '+this.gender);
    console.log('Color: '+this.color);
    console.log('Breed: '+this.breed);
    console.log('Emotion: '+this.emotion);
    console.log('Action: '+this.action);
};

However I only get literal code as String when logging out out on console.

Is there a Mistake I'm making? Or my web browser or IDE doesn't support that feature?

Steven Guerrero
  • 876
  • 8
  • 17

2 Answers2

1

You should use back tick ``. This is called template strings, variables inside the ${} is evaluated to their value. This also enables multi-line strings.

Cat.prototype.info = function(){
    console.log(`Name: ${this.name}`);
    console.log('Gender: '+this.gender);
    console.log('Color: '+this.color);
    console.log('Breed: '+this.breed);
    console.log('Emotion: '+this.emotion);
    console.log('Action: '+this.action);
};

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
0

You need to use backticks to do string-interpolation `` instead of ''

James Bass Davies
  • 408
  • 1
  • 3
  • 11