2

Here is the Python example

>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')

'Coordinates: 37.24N, -115.81W'

I am looking for a way to emulate this behavior in JavaScript. As I have plenty of templates strings as """{} lorem {} ipsum""" and bunch of JSON objects as k,v pairs that correspond to parameters & values.

I hate to implement my own parser if something equivalent already exist. Any similar libraries would be much appreciated.

2
  • 2
    You can consider using template strings for this purpose. Commented Jan 7, 2017 at 11:15
  • here. Commented Jan 7, 2017 at 11:16

1 Answer 1

5

In JavaScript this is called Template Literals. You can read about it here. It works similar to the Python format function but its done inline.

`string text ${some_variable} string text`

Where the ` back tick starts and ends the string. Inside the back ticks any ${variable} will be converted to the value of the variable. Just make sure the variable is in the scope of the string you are trying to format.

Sign up to request clarification or add additional context in comments.

1 Comment

Except it seems to lack all the template arguments that let Python actually control the format? Right-align, left-align, control padding character, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.