Explain how to Create Arrays in Java Script
https://www.computersprofessor.com/2016/06/explain-how-to-create-arrays-in-java.html
An
array is an ordered set of data elements which can be accessed through a
single variable name.
An
array is made up of a set of slots, it is with each slot assigned to a single
data element in the array. We can access the data elements either sequentially
(or) by their index.
Structure
of array.
® In java script an ‘array’ is a special type of object.
® An array is a data store.
Creating array:- Java script
arrays can be constructed n three different ways.
1) var arr = [“ sun “, “Mon”,
“Tue”];
® This creates an array of ‘3’
elements each holding a text string.
®Notice that the array of elements
are surrounded by “square” brackets”. the second approach is to create an
array object using the keyword “new” and set by set of elements to store.
2) var are = new Array (“sun”,
“Mon”, “Tue”);
3) Using this construct the contents
of an array are surrounded by parenthesis because they are parameters to the
constructor of the array object.
Finally
an empty array object which has space for no.of elements can be created.
3) var arr = new Array (5);
Java script arrays can hold mixed
data types as follows:-
4) var arr = new Array (“Surya”, 24,
3.14);
Adding elements to an array:-
Array elements are accessed by their index. The index denotes the position of
the element in an array a in “for” loops they are start from “Zero”.
Eg:- 1) arr [4] = “sun”; 2) arr [10] = 20;
Accessing array numbers:-
The elements in the array are accessed through their index values. The same
access method is used to find elements and to change their values.
If
you want to know how many elements have been stored in an array the ‘length’
attribute is used. The index no’s run from ‘o’ to ‘length-1’.
Eg:- var len = arr. length;
Object based array functions:- in
java script an array can act like an object.
concat (array 2.[array 3.[array n]]]:- A list of arrays are concatenated on to the
end of the array and a new array returned.
var
first = [“Mon”, “Tue”, 28, 3.14];
var
second = [“One”, “Tue”, 4];
var
result = first.concat (second);
Join (string):- Some times
all of the elements in an array joined together as a string. it parses
through the array creating a string of all elements”.In the resulting string the elements
are separated using the optional string parameters (i.e., “, “).
Eg:- first.join [“,”];
pop:- This function removes
the last element from the array and reduces the no.of elements in the array
by one.
Eg:-
first.pop( );
push:- Adds a list of
elements to the end of the array. The elements are separated using “,” in the
parameter list.
Eg:-
first.push (“ One”, “Two”, 89.14);
reverse:- As the name
suggest this function swaps all of the elements in the array.
Eg:- first.reverse ( );
Shift( ): Removes the first
elements of the array and shortens its length by one.
Syntax:- Shift( );
sort( ):- The array is
sorted into lexicographic dictionary order. Elements
in the array which are not text are first converted to strings. Then the
sort operation is performed.
Slice(start, [finish]): Sometimes you need to extract a range of elements from an array. The ‘Slice’
function is used to do this.
Two
parameters are possible. The first element which you want to remove is
specified in the just parameter. The last element you want is specified in
the second one.
If
you only give a single parameter all
elements from the specified one to the end of the array are. Selected.
Once
the elements have been sliced they are returned as a new array.The
original array is unaltered by this function.
Program:
<html>
<head>
<title> array operations </title>
</head>
<body>
<script language = “ java script”>
document.write(“<h1> array functions </h1>”);
var first = ‘[“Mon,”, “June”, 34, 76.34, “Wed”];
document.write (“ <p>);
document.write (first. Join (“,”));
document.write (“<br>”);
first. Pop ( );
document.write (first. Join (“,”));
document.write (“<br>”);
first. Push (“one”, “two”, “three”, 76.9);
document.write (first join (“ ,”));
document.write(“ <br> “);
first. shift ( );
first.shift ( );
document.write (first.join (“, “));
document.write(“<br>”);
first. shift ( );
first. shift ( );
document.write(first. Join (“,”));
document.write (“</p>”);
document. close ( );
</script>
</body>
</html>
o/p:-
Array functions
Monday, Tuesday, 34, 76.34,
Wednesday
Monday, Tuesday, 34, 7.34.
Monday, Tuesday, 34.64,34,, one,
two, three, 76.9.
7.69, Three, two, one, 76.4, 34,
Tuesday, Monday.
Two, One, 76.34,34, Tuesday, Monday. |