Home » JavaScript Array Methods tips, tricks and examples

JavaScript Array Methods tips, tricks and examples

Today, We want to share with you JavaScript Array Methods tips, tricks and examples.In this post we will show you javascript declare empty array, hear for javascript array methods we will give you demo and example for implement.In this post, we will learn about javascript array of objects with an example.

JavaScript Array Methods tips, tricks and examples

There are the Following The simple About JavaScript Array Methods tips, tricks and examples Full Information With Example and source code.

As I will cover this Post with live Working example to develop javascript array length, so the javascript object for this example is following below.

Javascript Arrays

JavaScript Array Literal

    var maganth=[];            // blank Array

JavaScript Array Constructor

var maganth=new Array();        // blank Array

Check Array datatype

var x=[];        
typeof x;            // return "object"
Array.isArray(x);    // return true 

Traversal values of array

var members=["sanjay","magan","tulshi"];
   
    members[0];        return "sanjay";  
    members[1];        return "magan";  
    members[2];        return "tulshi";  
    members[3];        return undefined;  

array.length

    var x=[];
    var y=["jan","feb","mar"];
    
    x.length;        return 0;                  
    y.length;        return 3;

Array length is mutable

var members=["sanjay","magan","tulshi"];
members.length;   //return 3
    
members.length=5;
members.length;   //return 5
       
members           //return ["sanjay","magan","tulshi",,] 2 empty
       
members.length=6;
members.length;   //return 6
       
members           //return ["sanjay","magan","tulshi",,,] 3 empty  

Array Methods

sort()

var members=["sanjay","magan","tulshi","weds","thakrshi","farendo","shailesh"];

members.sort();
members 
// return ["farendo", "magan", "shailesh", "sanjay", "thakrshi", "tulshi", "weds"] 

sort numeric array in alphabetical order

    var i=[1,2,3,10,20];    
    i.sort();
    i;  return [1,10,2,20,3];

Sort numeric array

    var i=[1,2,3,10,20];
    function srt(a,b){ retrun a-b }    
    i.sort(srt);
    i;  return [1,2,3,10,20];

array.reverse()

var members=["sanjay","magan","tulshi","weds","thakrshi","farendo","shailesh"];
members.reverse();
members;
    // ["shailesh", "farendo", "thakrshi", "weds", "tulshi", "magan", "sanjay"]

indexOf()

var members=["sanjay","magan","tulshi","weds","thakrshi","farendo","shailesh"];

members.indexOf("sanjay");         // return 0
members.indexOf("shailesh");         // return 6
members.indexOf("SUN");         // return -1

shift

var members=["sanjay","magan","tulshi"];

members.shift();        // return "sanjay"
members;               //return ["magan","tulshi"] 

unshift

var members=["sanjay","magan","tulshi"];

members.unshift("DAYS");     // return 4
members;                     //return ["DAYS","sanjay","magan","tulshi"] 

array.pop()

var members=["sanjay","magan","tulshi"];

members.pop();     // return "tulshi"
members;                     //return ["sanjay","magan"] 

array.push()

var members=["sanjay","magan","tulshi"];

members.push("weds");     // return 4
members;                     //return ["sanjay","magan","tulshi","weds"] 

array.splice()

var members=["sanjay","magan","tulshi"];

members.splice(0,1);  // remove 1 element from 0 index
members               // return  ["magan","tulshi"];

members.splice(1,1);  // remove 1 element from 1 index
members               // return  ["sanjay","tulshi"];

members.splice(0,2);  // remove 2 element from 0 index
members               // return  ["tulshi"];

members.splice(0,0,"members"); // add 1 element at 0 index
members                     // return  ["members","sanjay","magan","tulshi"];

members.splice(0,1,"members"); // add 1 element by removing element at 0 index
members                     // return  ["members","magan","tulshi"];

array.slice()

var members=["sanjay","magan","tulshi"];

members.slice(0);        return ["sanjay","magan","tulshi"];
members.slice(1);        return [magan","tulshi"];
members.slice(2);        return ["tulshi"];
members.slice(1,3);      return ["magan","tulshi"];

array.join()

var members=["sanjay","magan","tulshi"];

members.join();        return "sanjay,magan,tulshi";
members.join(":");     return "sanjay:magan:tulshi";
members.join("-");     return "sanjay-magan-tulshi";

array.concat()

var members1=["sanjay","magan","tulshi"];
var members2=["weds","thakrshi","farendo","shailesh"];

var members=members1.concat(members2);

members;  // return ["sanjay","magan","tulshi","weds","thakrshi","farendo","shailesh"]

split, string to array

var str="abc,efg";
   
    str=str.split("");      
                    // return ["a","b","c",",","e","f","g"]
                    
    str=str.split(",");
                   // return ["abc","efg"]

Multidimensional Array

    var members=[["g1","g2","g3"],["b1","b2","b3","b4"]];
    
    members.length;               // 2 arrays in array
    members[0];                   // first array in members
    members[1];                   // second array in members
    members[0].length              // 3
    members[1].length              // 4
    members[0][0];                // "g1"
    members[0][1];                // "g2"
    members[0][2];                // "g3"
    members[1][0];                // "b1"
    members[1][1];                // "b2"
    members[1][2];                // "b3"
    members[1][3];                // "b4"

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Restart Server from Linux (Ubuntu)

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about JavaScript Array Methods tips, tricks and examples.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  String Methods List using JavaScript Example
  2. Read Also:  How to integrate CardConnect payment Gateway with Node JS
  3. Read Also:  Pop Push Shift Unshift Array Methods
  4. Read Also:  Laravel Framework version Types
  5. Read Also:  Angular 7|8|9 Validate Mobile number With 10 Digits Example
  6. Read Also:  Multidimensional arrays in Laravel
  7. Read Also:  jquery multidimensional array search by value
  8. Read Also:  Get Radio Button value using jQuery Example
  9. Read Also:  AngularJS Confirm Password Validation Example
  10. Read Also:  Display JSON Data from database using php using jquery ajax

Leave a Comment

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