मान लीजिए निम्न हमारा ऐरे है:
var listOfNames = [ 'John', 'David', 'Bob' ];
निम्न हमारा बटन है:
<button id="reverse" onclick="reverseTheArray()">Click The Button To get the Reverse Value</button>
अब, ऐरे आइटम्स को रिवर्स में दिखाने के लिए, पहले ऐरे की लंबाई तक पहुंचें और लंबाई को 1 से घटाएं। फिर, रिवर्स ऑर्डर में विशेष इंडेक्स की सामग्री प्रिंट करें।
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <div align="center" id='reverseTheArray'></div> <button id="reverse" onclick="reverseTheArray()">Click The Button To get the Reverse Value</button> <script> var listOfNames = [ 'John', 'David', 'Bob' ]; count=listOfNames.length-1; function reverseTheArray(){ document.getElementById('reverseTheArray').innerHTML = listOfNames[count]; count--; if (count<0) count=listOfNames.length-1; } </script> </body> </html>
उपरोक्त प्रोग्राम चलाने के लिए, फाइल का नाम "anyName.html(index.html)" सेव करें और फाइल पर राइट-क्लिक करें। VS कोड एडिटर में "Open with Live Server" विकल्प चुनें।
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:

चरण 1
जब आप बटन पर क्लिक करते हैं, तो आपको ऐरे का अंतिम मान मिलेगा। यह निम्न आउटपुट उत्पन्न करेगा:

चरण 2
फिर से बटन पर क्लिक करें, आपको दूसरा अंतिम मान मिलेगा। यह निम्न आउटपुट उत्पन्न करेगा:
