I am new to JavaScript, I am trying to write a simple function to print array elements using while statement, but I am getting an extra undefined value at the end. Any help will be highly appreciated
Codes are:
var a = [1, 3, 6, 78, 87];
function printArray(a) {
if (a.length == 0) {
document.write("the array is empty");
} else {
var i = 0;
do {
document.write("the " + i + "element of the array is " + a[i] + "</br>");
}
while (++i < a.length);
}
}
document.write(printArray(a) + "</br>");
and the output is:
the 0element of the array is 1
the 1element of the array is 3
the 2element of the array is 6
the 3element of the array is 78
the 4element of the array is 87
undefined
How I am getting the undefined value? Am I skipping any index? Thanks in advance!