JavaScript Array pop() Method

JavaScript Array pop()

The JavaScript Array pop() Method is used to remove the last element of an array and returns the element that was removed. This function shortens the array’s length.

Syntax

arr.pop()

Parameters: This method  accept no parameter.

This method returns the array of removed elements. This function returns undefined if the array is empty.

Example 1

function func() {
        var arr = ['Niki', 'Tandel', 'Vision', 'Test'];
     
        // Popping the last element from the array
        console.log(arr.pop());
    }

Output

Test

Example 2

function func() {
    var arr = [];
 
    // popping the last element
    var popped = arr.pop();
    console.log(popped);
}

Output

undefined

Let’s take an exmaple to get file extension from URL/Path

Example

url = 'http://xy.com/blob/somefile.pdf'
filePath= 'c:\somefolder\somefile.txt'
ext1 = url.split(".").pop()
ext2 = filePath.split(".").pop()

OutPut

Submit a Comment

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

Subscribe

Select Categories