The Combine Website of all Practicals till 1 to 15 will upload Soon !!!
Practical 13 (1. Array)
Arrays in Javascript
Creating an Array | Javascript #1
Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...];
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";
cars[3]= "Porsche";
document.write(cars);
</script>
</body>
</html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = [
"Saab",
"Volvo",
"BMW"
];
document.write(cars);
</script>
</body>
</html>
Using the JavaScript Keyword new:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = new Array("Saab", "Volvo", "BMW", "Lamborgini");
document.write(cars);
</script>
</body>
</html>
Accessing Array Elements:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = ["Saab", "Volvo", "BMW", "abc"];
document.write(cars[3]);
</script>
</body>
</html>
Changing an Array Element:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Opel";
document.write(cars);
</script>
</body>
</html>
Converting an Array to a String:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango", "Pineapple"];
document.write(fruits.toString());
</script>
</body>
</html>
The length Property:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The length Property</h2>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;
document.write(size);
</script>
</body>
</html>
Accessing the First Array Element:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits[0]);
</script>
</body>
</html>
Accessing the Last Array Element:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits[fruits.length-1]);
</script>
</body>
</html>
Adding Array Elements:
Copied!
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The push() Method</h2>
<script>
const fruits = ["Banana", "Orange", "Apple"];
fruits.push("Lemon");