Arrays In JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SagarJaybhay
    New Member
    • Feb 2019
    • 17

    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:

    Code:
    var array = [];
    console.log("Array Length -- "+ array.length);



    Code:
    var array = new Array(100);
    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?
    Code:
    var array = [100,200,300]
    console.log("First Element -- " +array[0]);
    console.log("Last Element -- " + array[array.length-1]);
    
    document.write("Array Is " + array+"<br/>");
    
    document.write("First Element -- " + array[0] + "<br/>");
    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
    Code:
    var array=[10,20,30];
    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.
    Code:
    var constructor_array = new Array(2);
    constructor_array[0] = 10;
    constructor_array[1] = 20;
    constructor_array[3] = 30;
    console.log(constructor_array);
    console.log("Second Array Length - "+constructor_array.length);
    Last edited by gits; Jan 3 '20, 11:10 AM. Reason: fixed syntax that has to be correct in this kind of postings
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    while all this is trivial - the preferred method to declare an array in JavaScript is always with the array literals:

    Code:
    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:

    The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.
    Last edited by gits; Jan 2 '20, 08:31 AM.

    Comment

    • code2night
      New Member
      • Jan 2022
      • 1

      #3
      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!

      Hello guys, often while working with angular js you sometimes have requirement of implementing chat functionality where user can press @ and select users from the list , similar to what we do on skype or all big chatting platforms. For that purpose we can use Angular-mentions which is a package in Angular js for skype like @ chat functionality. We will see how to use that in detail.

      Comment

      • brycek379
        New Member
        • Feb 2022
        • 6

        #4
        Can I change an array length by mutating its length propery?

        Comment

        • ramisthand76
          New Member
          • Mar 2022
          • 1

          #5
          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.

          Comment

          Working...