I am unable to understand the behavior of for loop.
jquery:
$('#btnTest').on("click", function () {
var thumbUrl = "http:localhost/File/Modules/FileManager/Library/1041323042_12.jpg";
var arrurl = thumbUrl.split("/");
var i;
var result;
for (i = 0; i < arrurl.length - 1; i++) {
result += arrurl[i] + '/';
}
alert(result);
});
and html:
<input type="button" class="ThumbLink" value="Test" id="btnTest"/>
But alert gives a result:
undefinedhttp:localhost/File/Modules/FileManager/Library/
Where this undefined comes from.What is my mistake in for loop ?.Thanks.
My jsfiddle:
Maybe, since you’re simply appending to
resulteach time through the loop, you may want to set it initially to something other thanundefined.Making that change to your jsfiddle link results in the correct output: