Consider this code:
var age = 3;
console.log(\"I\'m \" + age + \" years old!\");
Are there any other ways to insert the value of a variabl
Supplant more for ES6 version of @Chris Nielsen's post.
String.prototype.supplant = function (o) {
return this.replace(/\${([^\${}]*)}/g,
(a, b) => {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
string = "How now ${color} cow? {${greeting}}, ${greeting}, moo says the ${color} cow.";
string.supplant({color: "brown", greeting: "moo"});
=> "How now brown cow? {moo}, moo, moo says the brown cow."