Javascript Format String

Nov 3, 2017

For ES2015, you can use Template/String literals.

TIPS: you can use Babel (or use babel-loader with Webpack) for cross-browser support.

var value = 'world'var str = `Hello ${value}`

It's basically an expression, so you could include function call, etc.

function addNumber(a, b) {    return a + b;}var a = 1;var b = 2;console.log(`${a} + ${b} = ${addNumber(a, b)}`);console.log(`${a} + ${b} = ${a + b}`);

Or, you can write your own String.format function.

// include any of the above implementation."Hello {0}".format("world")

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.