Javascript Data Type How to - Treat a string as array








Question

We would like to know how to treat a string as array.

Answer


<!--from  w  ww.j ava  2s.c o m-->
<!DOCTYPE html>
<html>
<body>
<script type='text/javascript'>

var string = '123'
document.writeln(string[1] == '2') // should alert true

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

The code above is rendered as follows: