array javascript

array javascript

In this tutorial we will learn about How to use JavaScript Array

How to create an JavaScript array :

<script>

var myarray = ['value1','value2'];

</script>

How to call array value using JavaScript

<script>

document.write(myarray[0])

</script>

if you want to print full array using loop here is the code for the same :

<script>
var myarray = ['value1','value2'];
for(a in myarray){
document.write(myarray[a]);
}
</script>

In the above example I have not used simple for loop in this loop a used to get all the keys of an array and there is no need to provide ending condition for the loop.

Now if we require length of the array here is the code for the same :

<script>
document.write(myarray.length);
</script>

To print an length of an array we need to use .length method in JavaScript as shown in the example above.

Next Array method is to add value in an array :

<script>
var newData = myarray.push('Value3');
document.write(newData.length);
</script>

In javaScript we need to use .push method to add new value into an array.

Now it’s time to remove values from an array There are 2 methods to use to perform this task:

Method 1 :

<script>
var newData = myarray.push('Value3');
myarray.pop();
for(a in myarray){
document.write(myarray[a]);
}
</script>

This method used .pop to remove value from an array and Value3 is removed as .pop method remove value from the last of an array. (How to use JavaScript Array)

Method 2 :

<script>
var newData = myarray.push('Value3');
myarray.shift();
for(a in myarray){
document.write(myarray[a]);
}
</script>

.shift method is also used to remove value from an array but there is a slight difference between pop and shift remove value from the front of the array.

Now here is the final example to create dropdown menu using JavaScript array :

<script>
var years = [‘2010′,’2011′,’2012′,’2013′,’2014′,’2015′,’2016′,’2017′,’2018’];
document.write(“<select>”);
for(a in years){
document.write(“<option>” + years[a] + “</option>”);
}
document.write(“</select>”);
</script>

30 thoughts on “array javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. You can certainly see your expertise within the work you write. The sector hopes for more passionate writers such as you who are not afraid to say how they believe. Always go after your heart.

  2. I am extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it’s rare to see a great blog like this one nowadays..

  3. Very good blog you have here but I was wanting to know if you knew of
    any community forums that cover the same topics talked about in this article?

    I’d really love to be a part of online community where I can get advice from other experienced people that share the
    same interest. If you have any suggestions, please let me know.
    Kudos!

  4. Someone necessarily help to make seriously articles I would state. This is the very first time I frequented your website page and so far? I surprised with the research you made to make this particular submit amazing. Excellent activity!

  5. I was wondering if you ever considered changing the
    structure of your site? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way
    of content so people could connect with it better. Youve got an awful
    lot of text for only having 1 or two images. Maybe you
    could space it out better?

  6. My family members every time say that I am killing my time here
    at net, except I know I am getting experience all the time
    by reading such pleasant articles or reviews.

  7. A motivating discussion is worth comment.
    There’s no doubt that that you ought to publish more about this
    issue, it might not be a taboo subject but typically folks don’t speak
    about such issues. To the next! Cheers!!

  8. Greetings! Very useful advice in this particular post!
    It is the little changes that produce the most significant
    changes. Many thanks for sharing!

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.