Array Object in JavaScript
- Introduction to JavaScript array
- Creating a JavaScript array
- JavaScript array object constructor
Introduction to JavaScript Array
An array is a collection of the same
type of values in JavaScript. A same type of value means either only strings or integers or floating-point numbers etc. The JavaScript array is used to avoid the
problem of creating many variables.
For example, if you want to store 200
employees, you do not need to create 200 variables. You can create an array in
which 200 names can be stored. By doing so, programmers save time.
Array Elements Indexing
Every element of the array can be
uniquely identified. Array stores values by indexing. But always remember
this thing that the array starts index with zero. Therefore, in the above
example, the first name will be on the zero indexes and the last name will be on
199 index. Any value of an array can be accessed by the name of the array and its
index number. You should always remember that the arrays in JavaScript are the
object. So you create them by using the new keyword.
Although JavaScript also gives you
the option of putting values directly, but still there is an array object in JavaScript.
Creating JavaScript Arrays
An array can be created by two ways in
JavaScript. Both of these methods can be used according to a different situation.
- Directly - In this way, you can create the array and put the value as well. These are the combined way of creating an array.
- With new keyword - In this manner, you create an array as an object. Where first of all, an array is created and thereafter values can be inserted. If you want, you can put values at the same time, for that you will have to use the constructor of the array object.
Directly (Combined Way)
Creating an array in this manner is
very simple. Its structure is given below.
Syntax
var array_Name = [value1, value2,
..... valueN];
In this method, the name of the array
is written after the var keyword. Array's name should be unique. After the name
of the array, the assignment takes place and thereafter values of an array will be
in angular brackets. Values are separated from the comma.
An example of this is being given
below.
Example1: Creating & initializing
array
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Array Example </title>
</head>
<body>
<script type="text/Javascript">
// Array declaration
var empName=["John","Jerry","Thomos","Juliya","Devid","Morgan","Tailor"];
// Printing array elements
for (i=0;i<7;i++)
{
document.write(empName[i], "\n");
}
</script>
</head>
<body>
<script type="text/Javascript">
// Array declaration
var empName=["John","Jerry","Thomos","Juliya","Devid","Morgan","Tailor"];
// Printing array elements
for (i=0;i<7;i++)
{
document.write(empName[i], "\n");
}
</script>
</body>
</html>
The above script will generate the below the web page.
With New Keyword
In this method, you can use the new
keyword to create an array in javascript. This is similar to creating an
object. Its structure is being given below.
Syntax
var array_Name = new Array ();
array_Name [0] = value1;
array_Name [1] = value2;
...
...
array_Name [n] = valueN;
If you want to put values simultaneously
then you can create an array from the below structure.
var array_Name = new Array (value1,
value2);
In the above method, the first array
has been created, followed by values given by index numbers. Values have
been put in the following way by the constructor of the JavaScript array of objects. These assign
values to constructor automatic index numbers.
Example2: New keyword in array
<!DOCTYPE html>
<html>
<head>
<title> JavaScript New Keyword Example </title>
</head>
<body>
<script type="text/javascript">
// Array declaration using new keyword
var empName=new Array ("Mariya","Adom","Pitar","Michal","William");
// Printing array elements
for (i=0;i<5; i++)
{
document.write(empName[i],"\n");
}
</script>
</head>
<body>
<script type="text/javascript">
// Array declaration using new keyword
var empName=new Array ("Mariya","Adom","Pitar","Michal","William");
// Printing array elements
for (i=0;i<5; i++)
{
document.write(empName[i],"\n");
}
</script>
</body>
</html>
The above script will generate the below the web page.
JavaScript Array Length Property
JavaScript array of objects provides you length
property. This property holds the length of your array. The
structure of access to this property is given below.
Syntax
array_Name.length;
If you wish, you can use this
property to control the loop. An example of this is being given below.
Example3: Loop to the array length
The above script will generate below the web page.
<!DOCTYPE html>
<html>
<head>
<title> Javascript Array Length Example </title>
</head>
<body>
<script type="text/javascript">
var empAge = new Array (22,26,44,24,30,25,40,50,55,60);
// Displaying length of array
document.write ("Length of array is:","\n" + empAge.length,"\n");
document.write (", Arrays are:");
// Iterating loop upto array length
for (var i=0; i<empAge.length; i++)
{
document.write ("\n" + empAge[i],"\n");
}
</script>
</head>
<body>
<script type="text/javascript">
var empAge = new Array (22,26,44,24,30,25,40,50,55,60);
// Displaying length of array
document.write ("Length of array is:","\n" + empAge.length,"\n");
document.write (", Arrays are:");
// Iterating loop upto array length
for (var i=0; i<empAge.length; i++)
{
document.write ("\n" + empAge[i],"\n");
}
</script>
</body>
</html>
The above script will generate below the web page.
<<-- PREVIOUS NEXT -->>
This is a very amazing post for cheap web hosting services. in this post, you have provided all the basic information regarding.
ReplyDeletewebsite builder for reseller
Awesome blog with lots of information. It is really helpful for all.
ReplyDeleteAngularJS Training in Chennai
AngularJS course in Chennai
Angular 6 Training in Chennai
Angular Training in Chennai
React JS Training in Chennai
PHP Training in Chennai
Web Designing course in Chennai
Tally course in Chennai
AngularJS Training in Velachery
AngularJS Training center in Chennai
Angular 7 Training in Chennai
AngularJS Interview Questions