Wednesday, August 31, 2011

JavaScript - How to display array values without knowing the array size?

There are times that we don't know the exact length or size of an array and we want to display it using any loop statements but to do this we need to know the exact length of the array.

So this snippet will make it possible for you to display even if you have no idea on the length and size of your array using the length() method of an array.

<script type="text/javascript">
  var animals = ("dog","cat","elephant","snake");

  for (var i=0;i<animals.length;i++) {
    document.write(animals[i] + "<br />");
  }
</script>

Good luck and Enjoy coding.

No comments:

Post a Comment