Home
11.
Can I create a multidimensional array in JavaScript?

You can create an array of arrays, which amounts to the same thing:
var myArray = [[1,2], [3,4], [5,6]];
alert(myArray[1][0]); // alerts '3'

12.
Is there any particular reason why I should use one sort of loop over another?

It’s true that there is usually more than one type of loop that will solve any particular programming problem. You can use the one you feel most comfortable with, though it’s usually good practice to use whichever loop makes the most sense (in conjunction with your chosen variable names) in the context of what your code sets out to do.

13.
Is there a way to stop the current trip around a loop and move straight to the next iteration?

Yes, you can use the continue command. It works pretty much like break, but instead of canceling the loop and continuing code execution from after the closing brace, continue only cancels the current trip around the loop and moves on to the next one.

14.
Should I always write object-oriented code?
It’s a matter of personal preference. Some coders prefer to always think in terms of objects, methods, and properties, and write all their code with those principles in mind. Others feel that, particularly for smaller and simpler programs, the level of abstraction provided by OOP is too much, and that procedural coding is OK.

15.
How would I use my objects in other programs?

An object’s constructor function is quite a portable entity. If you link into your page a JavaScript file containing an object constructor function, you have the means to create objects and use their properties and methods throughout your code.

16.
Is there a quick way to determine whether a node has any child nodes?

Yes, you can use the hasChildNodes() method. This method returns a Boolean value of true if the node has one or more child nodes, or false if not. Remember that attribute nodes and text nodes cannot have child nodes, so the method will always return false if applied to these types of node.

17.
Is Mozilla’s DOM Inspector the only tool of its type?

Not at all. Just about every browser has some DOM inspection tools built into the developer tools. However, Mozilla’s DOM Inspector gives a particularly clear view of the DOM hierarchy and the parameters of individual nodes; that’s why I presented it here.

18.
Is it better to use the DOM to insert and retrieve HTML, or innerHTML?

Each has its advantages and disadvantages. To insert a chunk of HTML into a document, using innerHTML is quick and easy. However, it returns no references to the code you’ve inserted, so you can’t carry out operations on that content very easily. DOM methods offer finer-grained control for manipulating page elements.
Wherever you use innerHTML, the same result is achievable using DOM methods, though usually with a little more code. Remember, too, that innerHTML is not a W3C standard. It is well supported currently, but there’s no guarantee that that will always be so.

19.
I’ve seen references on the Web to DOM Core and HTML DOM. What are these, and what are the differences between them?

The DOM Core describes a basic nucleus of DOM methods that are applicable not just to HTML pages, but also pages written in any similar markup language— XML, for example. HTML DOM is a larger collection of additional methods relating specifically to HTML pages. They do offer some shorthand ways of carrying out certain tasks, at the expense of making your code a little less portable to non-HTML applications.

20.
Why would a user turn off JavaScript?

Remember that the browser might have been set up by the service provider or employer with JavaScript turned off by default, in an effort to improve security. This is particularly likely in an environment such as a school or an Internet cafe.
Additionally, some corporate firewalls, ad-blocking, and personal antivirus software prevent JavaScript from running, and some mobile devices have web browsers without complete JavaScript support.