0

With PHP, one can do$string="some text {$aVar} and more text";instead of$string="some text ".$aVar." and more text";

Can something similarly be done with JavaScript, or do I need to dostring="some text "+aVar+" and more text";

user1032531
  • 24,767
  • 68
  • 217
  • 387

1 Answers1

1

Since JavaScript identifiers can't be distinguished from other parts of a string (there is no special symbol (like $) to mark them out), it has no interpolation features.

It also has nothing helpful for string formatting built in, so you have to resort to third party code or writing your own string formatters from scratch.

Basic string formatting can be done in many languages using sprintf syntax and JavaScript implementations of it are available. You could also look at full template languages such as Mustache.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335