The functionality of the back button is based on browser history which makes it easy for a user to go and back forth. But sometimes we do want to implement such functionality programmatically and allow a user to go back to the previous page on button click.
To accomplish our goal, we can make use of the History object. This object has various helpful methods and properties to manipulate history of browser session. One of those methods is the back() method which takes a user back to the previously navigated page dynamically. To learn more about History object, please check out MDN docs.
HTML & CSS
We have 3 HTML pages (index, page1, and page2).
The index page contains 2 anchor elements so that we can navigate to page1 and page 2.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.