This will never throw an error because pathname always starts with a /, so the minimum length of the resulting array will be two after splitting:
const firstPath = location.pathname.split("https://stackoverflow.com/")[1];
If we are at the domain root, the returned value will be an empty string ''
const path = location.pathname.split("https://stackoverflow.com/");
path = [
'',
'questions',
'8082239',
'get-the-first-part-of-a-url-path',
'8082346'
];