codegurukool.com · Free coding tutorials & online compiler

Home › HTML Tutorial › Web storage API

Web storage API

Keep data in the browser.

localStorage persists, sessionStorage lasts until the tab closes. Both store strings. This sandboxed preview blocks storage, so the example catches the error; it works on your own page.

Example code

<p id='o'></p>
<script>
try{localStorage.setItem('n','Ada');o.textContent='Saved: '+localStorage.getItem('n')}
catch(e){o.textContent='Storage blocked here: '+e.name}
</script>'

Run this code in the online editor

← Popover APIWeb workers API →