HTML Practical 13.1 | Array | Web-Programming | WebDevs I CS

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, ...];   
    <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:

    <html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = new Array("Saab", "Volvo", "BMW", "Lamborgini");
document.write(cars);
</script>
</body>
</html>

Accessing Array Elements:

    
        <html>
<body>
<h1>JavaScript Arrays</h1>
<script>
const cars = ["Saab", "Volvo", "BMW", "abc"];
document.write(cars[3]);
</script>
</body>
</html>

        
        

Changing an Array Element:

    
        <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:

    
        <html>
<body>
<h1>JavaScript Arrays</h1> 
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango", "Pineapple"];
document.write(fruits.toString());
</script>
</body>
</html>

        

The length Property:

    
        <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:

    
        <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:

    
        <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:

    
        <html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The push() Method</h2>
<script>
const fruits = ["Banana", "Orange", "Apple"];
fruits.push("Lemon");



        

Post a Comment

Previous Post Next Post