Md. Nazmul Hossain Bilash Md. Nazmul Hossain Bilash - 6 months ago 25
Javascript Question

how to concat string with dynamic value if that value exists

Suppose I have a variable


var data = 6;

If we have
value and it's greater than 5 then the output should be:

"Hello x, 6 is your promo code"

If we don't have a
value, or the value is less than 5 then the output should be:

"Hello x"

How can I do this with a single line of JavaScript?


Try this:

var numb = 12;
var msg = "Hello x" + (numb > 5 ? (', ' + numb + ' is your promo code') : '');