SharePoint - Get Current SiteCollection URL with _spPageContextInfo JavaScript Object

To get the URL of current site collection or current web of a SharePoint site in Javascript, we can use _spPageContextInfo object.

_spPageContextInfo is a simple object that we can find on every SharePoint 2010 or 2013 page, and in SharePoint 2013 it will have the following information on it:

However we can’t access _spPageContextInfo object from the App pages. But in that case most of these values can be accessed by using URLstrings and tokens in apps for SharePoint.