Difference between tab and window:
A
window can contain several tabs. If we store some data of a website in the session storage and we open the same website in a
new tab of the same
browser window then that data would not be available in the
new tab in session storage but if we store data in local storage and we open the same website in a new tab of the same window than the data would be available in the local storage in new tab also . Also , if we open the same website in a new browser window then local storage data would be available in the new window also.
for example :
In this website we have stored 2 keys for demo , 1 in session storage called 'dev1' and 1 localStorage 'dev2'. With the help of followig code :
createWebStorageDemo(): void{
console.log(sessionStorage['dev1']);
console.log(localStorage['dev2']);
if( !sessionStorage['dev1']){
sessionStorage['dev1'] = 'viresh';
console.log('dev1 set')
}
if(!localStorage['dev2']){
localStorage['dev2'] = 'Praveen';
console.log('dev2 set')
}
}
So , If now you would open the website in a new tab and you open the browser console , you would find the string 'dev1 set' printed in the console because the code doesn't find it in new tab and has to set it again. Whereas the key 'dev2' is present in localstorage so it will already be present if you open our website in a new tab or a new window.