Javascript - Data Types and Variables - Javascript Array
Ocak 12, 2011 by Javascript
|
|
Article Information
This Javascript tutorial contains and tries to cover following subjects:
- Brief explanation of array in Javascript
- Example to converting string to number in Javascript
Articles tries to provide answer to following questions and issues:
- How to create an array in Javascript
- How to store values in array in Javascript
Articles pre-requisites following information:
- General knowledge of variables in Javascript
- General knowledge of functions in Javascript
- General knowledge of HTML
Brief explanation of Javascript Array
In this javascript tutorial, we will try to cover javascript arrays subject to understand how to create an array, store data in them and access to array values with a html example.
Arrays, similar in other programming languages, can store values - data. They are some sort of data packages if we imagine. In earlier javascript tutorial javascript variables, we had explained javascript variables in detail.
Javascript arrays different from a normal javascript variable, can hold more than one data. For example, we can store some numbers, some text and similar.
How to create a Javascript array: In order to create a javascript array, we first need to create an javascript array variable, and then initialize it with Array() function with adding "new" before function. This tells javascript to create and initialize a new array. Lets look into creating array syntax in javascript:
var v_cityNames = new Array();
Similar to other programming languages, we can declare javascript array first, and initialize it after declaration. For example:
var v_cityNames ;
v_cityNames = new Array();
How to limit size of javascript array: to tell Javascript our array size, we provide it within parenthesis. For example, in order to create a javascript array which is a fixed size, syntax is as follows:
var v_cityNames = new Array (9);
Above javascript array means it can store 10 different variable inside array.
How to store data in javascript array: we created an array above. To store number, string or text in our array variable, we can provide data between parenthesis. For example, lets provide some data into our javascript array variable.
var v_cityNames = new Array ("London", "Glosgow");
We declared an javascript array, initialized and assigned data to array.
How to access positions (elements) in javascript array:
To access variable data in array, we use its index number. Index number starts from zero and increased by one per position in array. We can think index here as room number in a hotel. In above javascript array, we can access city elements as follows.
v_cityNames[0] ...returns "London"
v_cityNames[1] ...returns "Glowgow"
As above syntax indicates, we enclose position number (index number) between square brackets. Every accessed element is a variable in essence. That means we can initiate concatenation or similar tasks on that elements.
Example for a Javascript Array
In following javascript example, we will create above javascript array in HTML page in javascript block. We will declare array, initialize, assign some values and finally display them in HTML page.
Data Layers
Area: | programming \ languages \ javascript \ \ \ |
Ref: | josefh75 |
Loc: | articles |
Tags: | javascript |
|