Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:49:14+00:00 2026-05-15T00:49:14+00:00

The question is this: Create a page with a number of links. Then write

  • 0

The question is this:

Create a page with a number of links. Then write code that fires on the window onload event, displaying
the href of each of the links on the page.

And this is my solution

<html>
<body language="Javascript" onload="displayLink()">
<a href="http://www.google.com/">First link</a>
<a href="http://www.yahoo.com/">Second link</a>
<a href="http://www.msn.com/">Third link</a>

<script type="text/javascript" language="Javascript">
function displayLink()
{
 for(var i = 0;document.links[i];i++)
 {
 alert(document.links[i].href);
 }
}
</script>

</body>
</html>

This is the answer provided by the book

<html>
<head>
<script language=”JavaScript” type=”text/javascript”>
function displayLinks()
{
 var linksCounter;
 for (linksCounter = 0; linksCounter < document.links.length; linksCounter++)
 {
  alert(document.links[linksCounter].href);
 }
}
</script>
</head>
<body onload=”displayLinks()”>
<A href=”link0.htm” >Link 0</A>
<A href=”link1.htm”>Link 2</A>
<A href=”link2.htm”>Link 2</A>
</body>
</html>

Before I get into the javascript tutorial on how to check user browser version or model,I was using the same method as the example,by acessing the length property of the links array for the loop,but after I read through the tutorial,I find out that I can also use this alternative ways,by using the method that the test condition will evalute to true only if the document.links[i] return a valid value,so does my code is written using the valid method??If it’s not,any comment regarding how to write a better code??Correct me if I’m wrong,I heard some of the people say “a good code is not evaluate solely on whether it works or not,but in terms of speed,the ability to comprehend the code,and could posssibly let others to understand the code easily”.Is is true??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:49:15+00:00Added an answer on May 15, 2026 at 12:49 am

    In general, when looping through an array, you want to use its length property as the book’s solution did. Your solution should be just fine in this particular situation as well, but it has a weakness: It’s perfectly valid for an entry in an array to be 0, null, undefined, or false, all of which are “falsey” values and so document.links[i] could be false even though you weren’t at the end of the array.

    Example:

    var index, a;
    a = [3, 2, 1, 0, -1, -2];
    for (index = 0; a[index]; ++index) {
        alert(a[index]);
    }
    

    That will alert 3, 2, and 1, but then stop. Compare to:

    var index, a;
    a = [3, 2, 1, 0, -1, -2];
    for (index = 0; index < a.length; ++index) {
        alert(a[index]);
    }
    

    …which will alert 3, 2, 1, 0, -1, and -2.

    You might see code that looks like this: for (index in a). In general don’t use that to loop through array indexes, it’s based on a misconception of what for..in does. (More on that below.)

    (There is another, new way of looping through array entries has been added as of the new 5th edition specification: The forEach function. You give it a function, and it calls it for each element in the array. Details in this other answer.. Sadly, IE8 doesn’t support it, but it’s one of the things that can be “shimmed” — search for “es5 shim” for multiple options.)

    When learning about arrays, it’s important to know that Javascript arrays are very different from arrays in most other languages. For one thing, they’re not (necessarily) arrays at all; in fact, they’re just normal Javascript objects with a couple of special features added on. Javascript objects are property maps, they map keys to values. For instance:

    var obj = {foo: 1};
    

    That obj object maps the key "foo" (a string) to the value 1. You can access that property either by using a literal name in your code, or by using [] and a string. And of course, if you’re doing the latter, you can use any string (literal or from a variable or from an expression, etc.). So all of these have the same result:

    x = obj.foo;
    x = obj["foo"];
    name = "foo";
    x = obj[name];
    name = "o";
    x = obj["f" + name + name];
    

    …you get the idea; as long as what you use within the [] evaluates to a string, you can look up the value using that key. But Javascript also does implicit coercion, so this works:

    var obj = {"1": "one"};
    alert(obj[1]); // alerts "one"
    

    There I’ve mapped a property named "1" to the value "one". But then I look it up with obj[1], using a number rather than a string. That’s okay, the interpreter will turn it into a string for me and then do the key lookup.

    What does all of this have to do with arrays? This: Array indexes are just property names. A Javascript array is a normal object that maps keys to values, with these special features:

    • Whenever you set a property whose name can be interpreted as a number, if that number is greater than the current maximum index present in the array, the length property is changed. So:

      var a = ["zero"];
      alert(a.length); // alerts 1
      a[3] = "three";
      alert(a.length); // alerts 4, because the max index is now 3
      
    • Whenever you set length, if there are properties with numeric names that have a value greater than or equal to the new length, those properties are deleted from the object.

      var a = ["zero", "one", "two", "three"];
      alert(a[3]); // alerts "three"
      a.length = 3;
      alert(a[3]); // alerts "undefined", the "3" property has been deleted
                   // only the "0", "1", and "2" properties remain
      
    • They have various properties for functions they inherit from the Array.prototype, like join or splice.

    That’s it. Nothing at all like arrays in C, C++, Java, or most other languages.

    Since arrays are just objects with a couple of extra features, you can put other, non-numeric properties on arrays if you want to:

    var a = ["zero", "one", "two"];
    a.foo = "bar";
    alert(a[1]);      // alerts "one", 1 is implicitly coerced to "1"
    alert(a["1"]);    // alerts "one"
    alert(a.foo);     // alerts "bar"
    alert(a["foo"]);  // alerts "bar"
    

    And this is where the for..in thing breaks down: Because for..in does not loop through array indexes, it loops through property names:

    var a, name;
    a = [1, 2, 3];
    a.foo = "bar";
    for (name in a) {
        alert(name);
    }
    

    This alerts "1", "2", "3", and "foo" (in no particular order). You can see how if you’d assumed that it did just array indexes, you’d be in trouble! You can use it to loop array indexes, but it’s more complicated than it’s worth:

    for (name in a) {
        if (String(Number(name)) === name && a.hasOwnProperty(name)) {
            alert(name);
        }
    }
    

    That first checks to see if the property name is a number, and then checks to see that the property is defined on a itself, not the Array.prototype (remember that arrays inherit properties from the Array prototype). (To be fair, this latter check is probably not all that important; if someone is adding numerically-named properties to the Array prototype, they’re doing a very Bad Thing(tm).)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create a web page that for the purposes of this question
I have few question in this regard When you create an internet page, does
The answer to this SO question says that you can create an api by
I've decided to take the advice in this question: create-excel-chart-programmatically-in-php and NOT attempt to
According to this question I succeeded to create upload image, but now I need
I couldn't find this question on here: Is there a way to create a
N.B THIS QUESTION HAS BEEN UPDATED, READ FURTHER DOWN Hi, I want to create
Following the CSS style trick from this question I was able to create a
I don't know exactly how to put this question. I want to create a
This might be basic question but how do I create a list of lists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.