How to add item to array of integers in JavaScript

3 Answers

0 votes
<!DOCTYPE html>
<html>
<head></head>
<body>

<script type="text/javascript">
var arr = [1, 2, 3, 2, 5, 6, 6, 2];

arr.splice(2, 0, 100);

for (var i = 0; i < arr.length; i++) 
     document.write(arr[i] + " ");
</script>

</body>
</html>

<!--
run: 

1 2 100 3 2 5 6 6 2  

-->

 



answered Feb 13, 2016 by avibootz
0 votes
<!DOCTYPE html>
<html>
<head></head>
<body>

<script type="text/javascript">
var arr = [1, 2, 3, 2, 5, 6, 6, 2];

arr.splice(0, 0, 100);

for (var i = 0; i < arr.length; i++) 
     document.write(arr[i] + " ");
</script>

</body>
</html>

<!--
run: 

100 1 2 3 2 5 6 6 2  

-->

 



answered Feb 14, 2016 by avibootz
0 votes
<!DOCTYPE html>
<html>
<head></head>
<body>

<script type="text/javascript">
var arr = [1, 2, 3, 2, 5, 6, 6, 2];

arr.splice(1, 0, 800, 900);

for (var i = 0; i < arr.length; i++) 
     document.write(arr[i] + " ");
</script>

</body>
</html>

<!--
run: 

1 800 900 2 3 2 5 6 6 2   

-->

 



answered Feb 14, 2016 by avibootz

Related questions

3 answers 270 views
1 answer 150 views
3 answers 279 views
1 answer 169 views
1 answer 178 views
1 answer 183 views
1 answer 180 views
...