Get the first part of a url path

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'
];

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)