jadjoubran / codetogo.io

🚀 JavaScript code to go - Find updated snippets for common JavaScript use cases
https://codetogo.io
MIT License
233 stars 30 forks source link

How to output variables in a string in ES6 #6

Closed khalilsarabey closed 6 years ago

khalilsarabey commented 6 years ago
var myName = 'My name is ${fullname}.';
var url = 'http://mysite.com/api/user/${id}';
console.log(myName); //My name is Khalil
console.log(url); // http://mysite.com/api/user/123
jadjoubran commented 6 years ago

Thanks @khalilsarabey! We already have that use case here: How to interpolate in javascript

jadjoubran commented 6 years ago

Feel free to re-open if you've got further suggestions on this particular use case! Cheers

khalilsarabey commented 6 years ago

That's cool @jadjoubran ! I didn't check the entire list on the site :) I think you can take it a step further by providing an expression interpolation example like:

var calculate = '1+1 = ${ 1 + 1 }'; console.log(calculate); //1+1 = 2

jadjoubran commented 6 years ago

Awesome idea, will push this today! 😄

interpolate
jadjoubran commented 6 years ago

Updated use case is live 🎬 https://codetogo.io/how-to-interpolate-in-javascript/