JavaScript Array: Create, Aceess and Methods

ArrayList and JavaScript, How to declare and store an ArrayList inside JavaScript, create an array in javascript, add item to javascript array, delete an item in an array in javascript, find an item in an array in javascript, these are the very basic question which most of the new developer searching for so I decided to write an articled to understand what is array in javascript and how it can be used with some easy examples.

The JavaScript Array global object is a constructor for arrays, which are high-level, list-like objects.

Create an array, and assign values to it:

var carType = new Array();
carType [0] = "Sedan";
carType [1] = "Coupe";
carType [2] = "Convertible";

OR

var carType = new Array("Sedan","Coupe","Convertible");

OR

var carType = ["Sedan","Coupe","Convertible"];

Access an Array element:

var sedan = carType[0];
var indexofCoupe =  carType.indexOf("Coupe"); // gives 1
// Change the value from Coupe to VAN
carType[1] = "VAN";    //or
carType[indexofCoupe ] = "VAN";

Concatenate all the elements of an array

var strArray = carType.join(', ');

Combine two array to create a single array

var arr1 = ['A', 'B', 'C'];
var arr2 = ['X', 'Y', 'Z'];
var finalArray = arr1.concat(arr2);

Combine three array to create a single array

var arr1 = ['A', 'B', 'C'];
var arr2 = ['X', 'Y', 'Z'];    
var arr3 = [1, 2, 3];
var finalArray = arr1.concat(arr2, arr3);

How to find an item in the array: Let's write a very simple function which accept the array and item to find the item in entier array

function FindItem(arr,item) {
    return (arr.indexOf(item) != -1);
}
Hamden Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
  • javascript
  • array
By Hamden On 30 Dec, 12  Viewed: 697

Other blogs you may like

A handy tutorial on creating Page Load Progress Bar using Pace.js

A website isn't complete without the inclusion of all the right elements. Progress bar is one of the key components of every website. It informs the user about the total waiting time for a particular web page or a specific task to load. Apart from adding value to the website, the progress bar is... By Celin Smith   On 18 Feb 2015  Viewed: 4,510

JavaScript isNaN function and usage

In JavaScript we have a function isNan (Not a number) to check whether the value is a number or not. It takes only one parameter, value and returns true or false, we will check here some values and conditions to understand it better. It supports in all browsers. isNaN(123); // false --... By Jason Olivera   On 30 Mar 2013  Viewed: 383

Use JavaScript to hide and show text and graphics

You can show and hide the content to get attention of the user on a web page. it's a very useful thing for web designer and web developers. You can use JavaScript and CSS to achieve it. I am going to show some examples of it. There are many libraries available on internet having the feature to... By Hamden   On 30 Dec 2012  Viewed: 586

JavaScript getAttribute() Method

In JavaScript getAttribute() method is used to get the value of an attribute in an object. **Syntax:** object.getAttribute(attributename) **Parameter:** It takes only one parameter *attributename* **Example:** Suppose you have an anchor tag with following attributes <a... By Alicia Gonzalez   On 02 Dec 2012  Viewed: 551

How to retrieve data from database using JavaScript in asp.net

I was checking how to validate a user email from database without post back, using JavaScript. After some search and RND found the way to call the web service method with the help of Ajax and JavaScript. Let’s say we have a registration page on which we accept email from the user and we expect... By Ali Adravi   On 24 Jun 2012  Viewed: 11,999