0% found this document useful (0 votes)
24 views2 pages

"Text/javascript": Type 10 0

This document demonstrates various methods of the Array object in JavaScript including: - Creating and populating arrays - Concatenating arrays - Joining array elements with delimiters - Adding/removing elements from the end or beginning with push, pop, shift, unshift - Finding indices of elements with indexOf and lastIndexOf - Inserting/removing elements at a specified index with splice - Sorting arrays in ascending and descending order

Uploaded by

Spam Mail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

"Text/javascript": Type 10 0

This document demonstrates various methods of the Array object in JavaScript including: - Creating and populating arrays - Concatenating arrays - Joining array elements with delimiters - Adding/removing elements from the end or beginning with push, pop, shift, unshift - Finding indices of elements with indexOf and lastIndexOf - Inserting/removing elements at a specified index with splice - Sorting arrays in ascending and descending order

Uploaded by

Spam Mail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

<!

-- Array object and methods-->


<html>
<head>
<script type="text/javascript">
var a=new Array(10);
for(var i=0;i<a.length;i++)
{
a[i]=i+1;
document.write(a[i]+"<br>");
}

var parents = new Array("Jani", "Tove");

var children = ["Cecilie", "Lone"];


var family = parents.concat(children);
document.write(family+"<br>"); // Jani,Tove,Cecilie,Lone

//Example 2:
var brothers = ["Stale", "Kai Jim", "Borge"];
var family = parents.concat(brothers, children);
document.write(family+"<br>"); //Jani,Tove,Stale,Kai Jim,Borge,Cecilie,Lone

//Example 3:
var fruits = ["Banana", "Orange", "Apple" ];
document.writeln(fruits.join() + "<br >"); //Banana,Orange,Apple
document.writeln(fruits.join("+") + "<br >"); //Banana+Orange+Apple
document.writeln(fruits.join(" and ")+ "<br>"); //Banana and Orange and Apple

var fruits = ["Banana", "Orange", "Apple", "Mango"];


document.writeln(fruits.pop() +"<br>"); //Mango //remove the last item
document.writeln(fruits+"<br>");//Banana,Orange,Apple
document.writeln(fruits.push("Lemon","Pineapple")+"<br>" ); //5
document.writeln(fruits+"<br>");//Banana,Orange,Apple, Lemon,Pineapple
document.writeln(fruits.reverse()+"<br>");//Pineapple, Lemon, Apple, Orange,Banana
document.writeln(fruits.shift()+"<br>" ); // Pineapple
document.writeln(fruits+"<br>");// Lemon, Apple, Orange, Banana
document.writeln(fruits.unshift("Kiwi","Pineapple") +"<br>"); //6

// added 5th and 6th item


document.writeln(fruits+"<br>"); // Kiwi,Pineapple,Lemon,Apple,Orange,Banana
document.writeln("Search an array for the item Apple<br>");
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
document.writeln(a);
var fruits = ["Banana", "Orange", "Apple", "Mango","Apple"];
var a = fruits.lastIndexOf("Apple");
document.writeln(a);

document.writeln("<br>Add items to the array at a positions:<br>");


var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
document.writeln(fruits);
document.writeln("<br>At position 2, add the new items, and remove 1 item:<br>");
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");
document.writeln(fruits);
var n = ["10", "5", "40", "25", "100", "1"];
document.write(n.sort()+"<br>");

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});        
document.write(points+"<br>");
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
document.write(points+"<br>");

var person={fname:"John",lname:"Doe",age:25};  
var x;
for (x in person)
{
document.write(person[x] + " "); // John Doe 25
}
document.write("<br>");
for(y in fruits)
{
document.write(fruits[y]+" ");
}
</script>
</head>
<body>
</body></html>

You might also like