Cody Beaner Cody Beaner - 2 years ago 77
Javascript Question

New to JavaScript: Can I pass a List into this function and have it still work?

As the title says, I'm new to JavaScript and not completely sure on certain syntax's.

After looking for a while I found the following function:

function makeUL(array) {
// Create the list element:
var list = document.createElement('ul');

for (var i = 0; i < array.length; i++) {
// Create the list item:
var item = document.createElement('li');

// Set its contents:

// Add it to the list:

// Finally, return the constructed list:
return list;

All I need to know is if I pass a List of Strings into the functions parameter, will it still work, or when it says array is that defining what kind of variable it is? If it won't currently work with a List what needs to change?

Answer Source

You can pass any Array or Array-like object containing string items, no matter how the parameter is named, and your function will work as expected.

Array-like object is an object, that has a length property with value >=0 and properties 0..length-1. Array-like object offen does not implement Array methods and its type is 'Object', while the type of Array is 'Array'.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download