TL;DR I've written a library to handle this + it's edge cases, see https://github.com/JonasBa/recent-searches#readme for usage.
While all examples are technically ok, they do not cover the edge cases of storing recent searches such as implementing expiration, ranking or limits for LocalStorage space. The major con of LocalStorage that you need to consider is that it will always be scoped to the browser and device that is doing the searches. E.g. if you search on desktop and then search on your mobile, recent searches would not show up and vice versa. Same goes for different domains, as LocalStorage is scoped per domain.
Some of the edge cases that you should think about when implementing recent searches are:
Expiration: Consider that someone searches for a query iPhone, but has looked for a query repairing iPhone 1 month ago, that repairing iPhone query is likely obsolete.
Ranking: Same goes for ranking when doing prefix search, if a user has made a query "apple television" 3h ago and a query "television cables" 8h ago, and they now search for "television", you want to probably implement a ranking system for the two.
Safely managing LocalStorage: Just writing to LocalStorage will eventually result in a massive JSON that you'll need to parse every time, thus gradually slowing down your application until you hit the storage limit and loose this functionality.
The recent-searches library helps you tackle all of the above. It will help you with all of the above issues and allow you to build recent-searches really quickly!