Basic JavaScript Question:
Download Job Interview Questions and Answers PDF
How to loop through array in JavaScript?
Answer:
There are various way to loop through array in JavaScript.
Generic loop:
<script langugage="javascript">
var i;
for (i = 0; i < substr.length; ++i) {
// do something with `substr[i]`
}
</script>
ES5's forEach:
<script langugage="javascript">
substr.forEach(function(item) {
// do something with `item`
});
</script>
jQuery.each:
<script langugage="javascript">
jQuery.each(substr, function(index, item) {
// do something with `item` (or `this` is also `item` if you like)
});
</script>
Generic loop:
<script langugage="javascript">
var i;
for (i = 0; i < substr.length; ++i) {
// do something with `substr[i]`
}
</script>
ES5's forEach:
<script langugage="javascript">
substr.forEach(function(item) {
// do something with `item`
});
</script>
jQuery.each:
<script langugage="javascript">
jQuery.each(substr, function(index, item) {
// do something with `item` (or `this` is also `item` if you like)
});
</script>
Download JavaScript Interview Questions And Answers
PDF