Use a variable for the index of an array - Javascript Array Operation

Javascript examples for Array Operation:Array Index

Description

Use a variable for the index of an array

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  w  ww  .j  a va 2  s.co m*/
var arr = ['first', 'second', 'third'];
var value = document.getElementsByTagName('span')[0].innerHTML;
console.log(typeof value);
console.log(arr[value]);
    }

      </script> 
   </head> 
   <body> 
      <span>1</span>  
   </body>
</html>

Related Tutorials