Description:-

This function adds one or more elements to the original array’s beginning and returns the modified array’s length.

arrayUnshift( array, object );

Member Function Syntax:-

array.unshift( object );

Attributes:-

array:- This is a required argument, the name of the array object where an object is to be added. The original array.

object:- This is a required argument, This is a new object that needs to be added.

Example:-

<cfscript>
   marvelArray = [ "Black Widow", "Scarlet Witch", "Thor", "Black Panther", "Iron Man", "Spider-Man", "Vision", "Captain Marvel" ];

   writeOutput( "Old Array:-" );
   writeDump( marvelArray );

   newArrayLength = arrayUnshift( marvelArray, "Captain America" );

   writeOutput( "Modified Array Length:- #newArrayLength#<br>" );
   writeOutput( "Modified Array:-" );
   writeDump( marvelArray );
</cfscript>

Result:-

Old Array:-

array
1Black Widow
2Scarlet Witch
3Thor
4Black Panther
5Iron Man
6Spider-Man
7Vision
8Captain Marvel

Modified Array Length:- 9

Modified Array:-

array
1Captain America
2Black Widow
3Scarlet Witch
4Thor
5Black Panther
6Iron Man
7Spider-Man
8Vision
9Captain Marvel

An example by using a member function:-

<cfscript>
   marvelArray = [ "Black Widow", "Scarlet Witch", "Thor", "Black Panther", "Iron Man", "Spider-Man", "Vision", "Captain Marvel" ];

   writeOutput( "Old Array:-" );
   writeDump( marvelArray );

   newArrayLength = marvelArray.unshift( "Captain America" );

   writeOutput( "Modified Array Length:- #newArrayLength#<br>" );
   writeOutput( "Modified Array:-" );
   writeDump( marvelArray );
</cfscript>

Result:-

Old Array:-

array
1Black Widow
2Scarlet Witch
3Thor
4Black Panther
5Iron Man
6Spider-Man
7Vision
8Captain Marvel

Modified Array Length:- 9

Modified Array:-

array
1Captain America
2Black Widow
3Scarlet Witch
4Thor
5Black Panther
6Iron Man
7Spider-Man
8Vision
9Captain Marvel