470,647 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Arrays In JavaScript

An array is a collection and it is zero-indexed. The meaning of this is the first element is at index zero and the last element is at an index of array length -1 position.
The array in JavaScript has length property which returns the size of that array.
Example:

Expand|Select|Wrap|Line Numbers
  1. var array = [];
  2. console.log("Array Length -- "+ array.length);



Expand|Select|Wrap|Line Numbers
  1. var array = new Array(100);
  2. console.log("Array Length -- "+ array.length);
In this example we only initialize the array and we are not adding any element in array. If you see the output you will see we initialize at 100 and length is also 100.

How to retrieve first and last element in array?
Expand|Select|Wrap|Line Numbers
  1. var array = [100,200,300]
  2. console.log("First Element -- " +array[0]);
  3. console.log("Last Element -- " + array[array.length-1]);
  4.  
  5. document.write("Array Is " + array+"<br/>");
  6.  
  7. document.write("First Element -- " + array[0] + "<br/>");
  8. document.write("Last Element -- " + array[array.length - 1] + "<br/>");
Different ways to declare array in javascript?

1) Declaring and populating array at the same time
Expand|Select|Wrap|Line Numbers
  1. var array=[10,20,30];
  2. console.log(array);
2) Declaring array with array constructor: in this method we declare array with array constructor and then populate this array with the index. In javascript the array are not fixed length array type in other languages like c#,java this will grow dynamically even though they will declared with fixed length.
Expand|Select|Wrap|Line Numbers
  1. var constructor_array = new Array(2);
  2. constructor_array[0] = 10;
  3. constructor_array[1] = 20;
  4. constructor_array[3] = 30;
  5. console.log(constructor_array);
  6. console.log("Second Array Length - "+constructor_array.length);
Dec 31 '19 #1
4 16990
gits
5,390 Expert Mod 4TB
while all this is trivial - the preferred method to declare an array in JavaScript is always with the array literals:

Expand|Select|Wrap|Line Numbers
  1. let arr = [];
there is no reason to allocate memory for elements that are not yet in the array since it can grow dynamically. Even tho other possibilities exist - in all the years i am working with JavaScript already - i never had a reason to use another way of declaring an array as by the literals.

A comprehensive doc about arrays, and especially the methods it provides, can be found here:

https://developer.mozilla.org/en-US/..._Objects/Array
Jan 2 '20 #2
code2night
1 Bit
When I started to code and didn't found the solution to tag people like whatsapp and skype by using @ in Angular JS. So here, I have completed my code and want to help otherone. So if you want to also apply this code then visit my official website. You can found all type of coding for all languages specially for Jquery, ASP.net and core. Here I am sharing the link where you can copy the code. Don't forget to comment on my blog and share your valuable feedback. So that I can help more developers. Also share your problems that yor are facing currently and didn't get solution. I will try to post the blog on those problems with solution. Thanks!

https://code2night.com/Blog/MyBlog?i...-in-Angular-js
Jan 18 '22 #3
brycek379
6 Nibble
Can I change an array length by mutating its length propery?
Mar 15 '22 #4
ramisthand76
1 Bit
Arrays can either hold primitive values or object values. An ArrayList can only hold object values. You must decide the size of the array when it is constructed. You can't change the size of the array after it's constructed.
Mar 16 '22 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by ABC | last post: by
34 posts views Thread by Christopher Benson-Manica | last post: by
104 posts views Thread by Leszek | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
11 posts views Thread by Bosconian | last post: by
3 posts views Thread by sjsean | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.