Skip to content Skip to sidebar Skip to footer

Javascript Var In String

So I have this code and would like to know how I could go ahead to put my javascript var into this string. I cannot seem to make a working code for myself. For the image source i w

Solution 1:

Try using ES6 new feature called Template literals (using quote). It is more cleaner and simpler.

var picture = {'value':'test-src-location'};
var text = `<img src="${ picture.value }">`;
console.log(text);

Solution 2:

Assuming your picture.value is not empty.

var text = '<img src=' + picture.value + '>';

Example: https://jsfiddle.net/no293etL/

Post a Comment for "Javascript Var In String"