I'm new to the raw JavaScript side of web development so I decided to learn. I know the basics of programming and I've ran into something I can't seem to fix. While playing with arrays in JavaScript I wanted to make a function to print everything out So I created something like this.
<!DOCTYPE html>
<html>
<head>
<title> Learning JavaScript </title>
<script>
var names = new Array("Connor <br />", "Shane <br />", "Ty <br />", "Brandon <br />");
function printArray (){
for(x=0; x < names.length; x++)
document.write(names[x]);
}
</script>
</head>
<body>
<script>
document.write(printArray());
</script>
</body>
</html>
It prints out the names but after the names it also has another piece of text called 'undefined'. It looks like this.
Connor
Shane
Ty
Brandon
undefined
I've tired moving the condition around a bunch in the for loop and getting no where. I think for me it would be great to understand why the 'undefined' is there and how I could possibly avoid these situations in the future while I'm actually doing a project. Any help would be great. Thanks :)