I am going to demonstrate a simple solution for this problem that is beginner-friendly and easy to understand. To get the count of items in an array, we can make use of the length property. We just need to do basic subtraction and division on that count to accomplish our goal.
In the following example, we have an array with an odd length. Upon click of a button, we will find the middle element in the array and display that on the screen. Please have a look over the code example and the steps given below.
HTML & CSS
We have 3 elements in the HTML file (div, button, and h1). The div element is just a wrapper for the rest of the elements.
The button element has “Get” and the h1 element has “Result” as innerText.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.