Why is there “undefined” text in the beginning of my string?

You’re not initializing your variable, so its value is undefined. Concatenating a string coerces it to the string "undefined" before the concatenation.

Consider:

var x
alert(x + "test") // undefinedtest

Instead, initialize your variable to an empty string before performing concatenation:

var x = ""
alert(x + "test") // test

Note that functionally it is much cleaner to first extract the property you’re interested in and then simply join them together:

$.map(vendor, function (v) { return v.vendor_id }).join('')

Leave a Comment