JavaScript Array examples

JavaScript Practise program and examples, hands-on examples in JS array, below are Java Script program to manipulate Array elements like

  • Find the most frequent item in JavaScript
  • Find the second largest number in Array using JavaScript
  • Delete an element from an array in JavaScript
  • Array soring in JavaScript
  • Replace an array element value in JS

JS code to display the most frequent item

<!DOCTYPE html>
<html>
<head>
<title>JavaScript program to find the most frequent item of an array</title>
</head>
<body>
    <h1>Most Frequent Items</h1>
    <h2 id="items"></h2>
    <script>
        var arr1=[1, 'c', 'c', 'c', 2, 3, 'c', 3, 2, 4, 7, 3];
var mf = 1;
var m = 0;
var item;
for (var i=0; i<arr1.length; i++)
{
        for (var j=i; j<arr1.length; j++)
        {
                if (arr1[i] == arr1[j])
                 m++;
                if (mf<m)
                {
                  mf=m; 
                  item = arr1[i];
                }
        }
        m=0;
}
document.getElementById("items").innerHTML=(item+" ( " +mf +" times ) ") ;

    </script>
</body>
</html>

Output

In this way, we learned how to find the most frequent array in JS

JS code to find the second largest number in Array

Problem statement

In this, you have given an array of numbers you have to find the second largest number in that array.

Solution

  • Create HTML extension file
  • Take an array of numbers
  • Use function nextbiggest()
  • Create for loop for (const value of arr)
  • Run the program console.log(nextBiggest(arr));

JS code to find the second largest number in an array

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Second largest number in Array</title>
</head>
<body>
    <script>
function nextBiggest(arr) {
  let max = -Infinity, result = -Infinity;

  for (const value of arr) {
    const nr = Number(value)

    if (nr > max) {
      [result, max] = [max, nr] // save previous max
    } else if (nr < max && nr > result) {
      result = nr; // new second biggest
    }
  }

  return result;
}

const arr = ['20','120','111','215','54','78'];
console.log(nextBiggest(arr));
</script>
</body>
</html>

Output

 

JavaScript code to sort an array

Problem statement

In this, you have given an array and you have to sort them in order.

Steps

  • Create HTML file as .HTML extension
  • Create a onclick Button
  • Create array
  •  Use function myFunction() {
    points.sort(function(a, b){return a – b});
    document.getElementById(“demo”).innerHTML = points;
    }
  • Run HTML file

JS code to sort the array

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>
<p> Sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;  

function myFunction() {
  points.sort(function(a, b){return a - b});
  document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>

Output

 

JavaScript program to delete an element from an array

Problem statement

In this, you have given an array and you have to delete the array using the JS function.

Steps

  • Create HTML file
  • Create array
  • Use JS function var shifted = arr.shift()
  • Run HTML file

JS code to delete an element from an array

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Remove array elemet</title>
</head>
<body>
<script>
function func() {
    var arr = ["One", "Two", "Three", "Four"];
 
    // Removing the first element from array
    var shifted = arr.shift();
    document.write("Removed element: " + shifted + "<br>");
    document.write("Remaining elements: " + arr);
}
func();
</script>  
</body>
</html>

Output

JS Program to replace an array element value

Program statement

In this, we have an array of elements and you have to replace the array element value.

Steps

  • Create HTML file
  • Use button
            button onclick="element_replace()"
  • Use function function element_replace()
  • Create a list of elements in an array
  • Run HTML file

Javascript code to replace an array element value

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <p>Click on the button to replace the array elements.
        </p>
        <button onclick="element_replace()">
            Replace
        </button>
        <p>The Updated Array Elements:</p>
        <p id="result"></p>
        <script>
            function element_replace() {
  
              // Initializing the array
              var list = ["January", 
                          "March",
                          "April",
                          "June"];
  
              list.splice(1, 0, "February");
              list.splice(4, 1, "May");
  
              document.getElementById(
                "result").innerHTML = list;
            }
        </script>
  
    </center>
</body>
  
</html>

Output