The pop() method removes (pops) the last element of an array and returns that element. The pop() method changes the original array. The pop() method returns the removed element.
Return
extension from string:
Var ext = url.split(‘.’).pop();
Above
line of code will split a string on ‘.’ (dot or as per required splitter) to create an array of words, and then return the last word.
Some
more examples:
var
str = "Hello World";
var
lastElement = str.split(/\s +/).pop();
In
which case lastWord would be "World".
If you did that one step at a time:
var
str = "Hello World";
var
elmArray = str.split(/\s +/);
Now
words is the array: ["Hello", "World"]
Then:
var
lastWord = elmArray.pop();
Now
lastWord is the last item from the array, i.e.,
"World".
The.pop()
method also actually removes the last item from the array and returns it.
Another
way to get the last word from a string is as follows:
var lastWord = someString.substr(someString.lastIndexOf(" ") + 1);