Left Quote    You have to THINK anyway, so why not think BIG?
- Donald Trump    
Right Quote
[login] | [Register]

Learning JavaScript Basics

by: bs0d
Page: 4 of 10
(View All)


If you are unfamiliar to what an array is, an array is just a collection of data that is referred to by a common name. Arrays in JavaScript start with element "0." Arrays can offer you a convenient way to group related variables. Also, in JavaScript, data items in arrays can be of different data types. This is unlike most programming languages. Furthermore, JavaScript array elements are not required to be of the same data type. This means, you could have one element as an integer, the next Boolean, another as float and so on. However, if you plan on doing calculations with array elements, the elements must be of the same data type.

Here are some useful advantages of using arrays:
  • Less amount of coding for the programmer.
  • Program will take up less memory, thus executes faster.
  • Makes it easier to process related groups of data.


In JavaScript, it is required to declare any array before it can be used. You can accomplish this by assigning the array a name, ='s sign and the keyword NEW, Then Array (capital A) and the # of elements in parenthesis you plan to use.


<arrayname> = new Array(<# of elements>);

myArray = new Array(50);

*Just make sure that you capitalize the "A" in array on the right hand side of the equals sign. If not, you will get an error.

Now just to sort of discuss why arrays are important and useful. What if you needed to get the average of say, 50 numbers? Without an array, you would write 50+ lines of code. On the other hand, with an array you can accomplish this by 7 or 8 lines of code. And that would be the same if you needed to find the average of 100000 numbers. Instead of writing 100000 lines of code, 7 or 8 will do the trick.

Example of using an array to average numbers:

*In the code sample we used a for loop. We will discuss these a bit later.

Array Length's

JavaScript allows you to find the number of elements in your array with the LENGTH property. This will show you the number of elements in the array. The LENGTH property is a statement just like a variable. Here is how you would use it on an array named, "theArray."


Joining Array Elements

Ok, what if you had an array and you wanted to combine each element to form a single sentence? Well, with the join(); function, it is quite simple! The join function will convert your array elements to a string. And a comma (,) is inserted between each element as it is converted. However, you can specify a different separator.


This would output: How are you this morning?

Notice my join function. it has ' ' included. This makes it output a space between each element. Also doing just, .join(" "); would work. But if nothing was inside the ( and ), it would output: How,are,you,this,morning? It defaults with the comma. If you want anything different, you must specify it. Just make sure you surround it with ' and ' because just .join( ) would make an error.

Sorting Array Elements

You have the ability with JavaScript to sort your array elements, and this is accomplished with the SORT property. It will sort them in ascending alphabetical order. They are arranged according to their ASCII character set. ASCII maps out each character with a combination of eight 0's and 1's. Each letter and number has an ASCII value associated with it.

If sort(); is used on numbers, the numbers are sorted alphabetically instead of numerically. So say an array with: 1,2,3,4,11,22 would sort as: 1, 11, 2, 22, 3, 4. So basically sort(); has to be used with strings.

Here is an example of an array and the sort(); property:

This would output: apples,banannas,mangos,oranges,pears

Just as the .join(); function, you can enter html between the ( and ). So, if you put, .sort('<br>'); this would output each item on a line by itself. And using .sort(" "); would put a space between each item instead of a comma.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

3 Total Comments
4.66 Rating of 5 (3 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· Getting Started with Ajax
Your Article Here

"" Copyright © 2002-2020; All rights lefted, all lefts righted.
Privacy Policy