Slice and Splice Array tutorial in JavaScript

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Slice and Splice Array Methods JavaScript</title>
    <script>
        // Index will be count as shown for below Array :
        // var a = [0, 1, 2, 3, 4];
        // var a = [-5, -4, -3, -2, -1];
        var a = ["Sanjay", "Aman", "Rehman", "Rahul", "Karan"];
        document.write(a + "<br><br>");
        var b = a.slice(1, 4);
        var c = a.slice(1);
        var d = a.slice(2);
        var e = a.slice(3);
        var f = a.slice(-1);
        var g = a.slice(-2);
        var h = a.slice(-3);
        var i = a.slice(-4, -2);
        var j = a.slice(-4, -1);
        document.write(b + "<br><br>");
        document.write(c + "<br><br>");
        document.write(d + "<br><br>");
        document.write(e + "<br><br>");
        document.write(f + "<br><br>");
        document.write(g + "<br><br>");
        document.write(h + "<br><br>");
        document.write(i + "<br><br>");
        document.write(j + "<br><br><br><br>");

        // Using "splice()" we can add or delete value from existing array
        // Using "slice()", we can make a new array of any selected value from existing array
        var k = ["Sanjay", "Aman", "Rehman", "Rahul"];
        document.write(k + "<br><br>");
        // k.splice(2,0,"Neha","Karan");
        document.write(k + "<br><br>");
        // k.splice(2,1,"Neha","Karan");
        document.write(k + "<br><br>");
        // k.splice(2,2,"Neha","Karan");
        document.write(k + "<br><br>");
        // k.splice(-2,1,"Neha","Karan");
        document.write(k + "<br><br>");
        k.splice(2, 2);
        document.write(k + "<br><br>");
    </script>
</head>

<body>
</body>

</html>



Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript