Sunday, January 22, 2023

JavaScript Practicals

Que : Write a Program in JavaScript to PUSH a single value inside an array.

Ans. : See  below 


<!DOCTYPE html>

<html>

<head>

<style ="text/css">

 body{margin:150px;

      font-size:30px;

     }

</style>  

</head>

<body>

<p id="demo"> This is to define the ID </p>

<button onclick="pushing()">try me</button>

<script>

 var a = new Array();

a[0] ="zero";   

a[1]="one";

a[2]="two";

a[3]="three"

a[4]="four";

 

function pushing(){

a.push("five"); // Pushing the value five at the end i.e a[5]

        var x = document.getElementById("demo");

    x.innerHTML=a;

}

</script>

</body>

</html>


Output 

1st Screen




After clicking the try me option screen pushed five in array a[5] and output


zero,one,two,three,four,five

Try me