Wednesday, July 3, 2024
HomeLanguagesJavascriptTypeScript | Array shift() Method

TypeScript | Array shift() Method

The Array.shift() is an inbuilt TypeScript function which is used to remove the first element from an array and returns that element. 
Syntax:

array.shift(); 

Parameter: This methods does not accept any  parameter. 
Return Value: This method returns the removed single value of the array. 
Below example illustrate the  Array shift() method in TypeScriptJS:

Example 1: 

JavaScript




<script>
   // Driver code
    var arr = [ 11, 89, 23, 7, 98 ]; 
  
    // use of shift() method 
    var val = arr.shift();
       
    // printing
    console.log( val );
    console.log( arr );
</script>


Output: 

11
[ 89, 23, 7, 98 ]

Example 2: 

JavaScript




<script>
   // Driver code
    var arr = [2, 5, 6, 3, 8, 9]; 
    var val;
   
    // use of reverse() method 
    val = arr.shift();
    console.log( val );
    val = arr.shift();
    console.log( val );
       
    // printing
    console.log( arr );
</script>


Output: 

2
5
[ 6, 3, 8, 9 ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments