Timeline for JavaScript equivalent to printf/String.Format
Current License: CC BY-SA 4.0
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| S Mar 14, 2023 at 19:42 | history | suggested | CommunityBot | CC BY-SA 4.0 |
integrate the comment from @Tustin2121 in the answer since it's useful
|
| Mar 12, 2023 at 19:45 | review | Suggested edits | |||
| S Mar 14, 2023 at 19:42 | |||||
| Nov 7, 2022 at 0:36 | history | edited | AMJ | CC BY-SA 4.0 |
fixed grammar
|
| Mar 12, 2021 at 9:16 | comment | added | Alex Povolotsky | It's nothing close to sprintf. In sprintf, I can use, say, %.2f to print money - but template string is just a dumb wrapper around string concatenation... | |
| Apr 25, 2019 at 10:28 | comment | added | Dragas | Template strings do not feature the formatting capabilities, hence why they're uncomparable in that regard. Ex. specifying argument width, or making sure that argument has exact precision. | |
| Feb 6, 2019 at 0:20 | comment | added | naught101 | caniuse.com/#feat=template-literals suggests there are still >10% of net denizens using browsers that don't support this. Does it degrade nicely, or explode? | |
| Oct 22, 2017 at 10:28 | comment | added | bracco23 | +1 for using the standard built in function which can be helpful in a lot of situations. here a list of polyfills for supporting older browsers | |
| May 4, 2017 at 18:44 | comment | added | styfle | @inanutshellus That works fine if your template function is defined on the same machine where it is executed. As far as I know, you can't pass a function as JSON, so storing template functions in a database doesn't work well. | |
| Nov 15, 2016 at 14:06 | comment | added | Mike | this solution won't work for format string passed in variable (from server for example) | |
| Aug 22, 2016 at 17:46 | history | edited | Kim | CC BY-SA 3.0 |
edited body
|
| May 27, 2016 at 16:13 | comment | added | Tustin2121 | Actually, I've since looked at the Mozilla link given in this answer and see that their example tagged template that returns a function isn't a bad way to do i18n-like string tables, where you can pass the returned function the parameters you want to put into the string. Granted, I don't know how that compares in memory size to simply strings you interpolate by replacing {0} markers. | |
| May 27, 2016 at 6:22 | comment | added | cchamberlain |
@Tustin2121 there is no difference between using a template string or old style string concatenation, its sugar for the same thing. You would have to wrap an old style string generator in a simple function and the same thing works fine with string templates. const compile = (x, y) => `I can call this template string whenever I want.. x=${x}, y=${y}` ... compile(30, 20)
|
|
| May 25, 2016 at 3:51 | comment | added | inanutshellus | @Tustin2121 You're right that they're not built to be assigned to a variable, which is a bit mind-warping, but it's easy enough to work with templated strings' instant-execution tendancies if you hide them in a function. See jsfiddle.net/zvcm70pa | |
| Dec 4, 2015 at 23:39 | comment | added | Tustin2121 | The problem with template strings is that they seem to be executed immediately, making their use as, say, an i18n-like string table completely worthless. I can't define the string early on, and supply the parameters to use later and/or repeatedly. | |
| S Aug 25, 2015 at 11:03 | history | answered | Kim | CC BY-SA 3.0 | |
| S Aug 25, 2015 at 11:03 | history | made wiki | Post Made Community Wiki by Kim |