Single dimensional array vs multidimensional array in JavaScript.



Following is the code for single and multidimentsional array in JavaScript −

Example

 Live Demo

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style>    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,.sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } </style> </head> <body> <h1>Single dimensional array vs multidimensional array</h1> <div class="sample">Single dimensional Array<br /></div> <div class="result">Multidimensional Array<br /></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to display single and multidimensional array</h3> <script>    let resEle = document.querySelector(".result");    let sampleEle = document.querySelector(".sample");    let BtnEle = document.querySelector(".Btn");    let arr = [1, 2, 3, 4, 5];    let arr1 = [       [1, 2, 3],       ["A", "B", "C"],       [5, "D", 6],    ];    BtnEle.addEventListener("click", () => {       sampleEle.innerHTML += arr + "";       arr1.forEach((item, index) => {          resEle.innerHTML += "Index : " + index + " array = " + item + "";       });    }); </script> </body> </html>

Output

The above code will produce the following output −

On clicking the ‘CLICK HERE’ button −

Updated on: 2020-07-21T07:12:47+05:30

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements