Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Web Storage API

The Web Storage API offers a straightforward syntax for storing and retrieving data in the browser, making it easy to use.

Example

localStorage.setItem(“name”“John Doe”);
localStorage.getItem(“name”);

The Web Storage API is compatible with all modern browsers.

DOM 1

The localStorage Object

The localStorage object provides access to local storage for a specific website, allowing you to store, read, add, modify, and delete data for that domain.

The data has no expiration date and is not deleted when the browser is closed.

It can persist for days, weeks, or even years.

The setItem() Method

The localStorage.setItem() method stores a data item in the storage, accepting a name and a value as parameters.

Example

localStorage.setItem(“name”“John Doe”);

The getItem() Method

The localStorage.getItem() method retrieves a data item from storage, taking the item’s name as a parameter.

Example

localStorage.getItem(“name”);

The sessionStorage Object

The sessionStorage object is similar to the localStorage object, but it stores data for a single session.

The data is deleted when the browser is closed.

Example

sessionStorage.getItem(“name”);

The setItem() Method

The sessionStorage.setItem() method stores a data item in session storage, accepting a name and a value as parameters.

Example

sessionStorage.setItem(“name”“John Doe”);

The getItem() Method

The sessionStorage.getItem() method retrieves a data item from session storage, taking the item’s name as a parameter.

Example

sessionStorage.getItem(“name”);

Storage Object Properties and Methods

 

Property/Method

Description

key(n)

Returns the name of the nth key in the storage.

length

Returns the total number of data items stored in the Storage object.

getItem(keyname)

Returns the value associated with the specified key name.

setItem(keyname, value)

Adds a key to the storage or updates its value if the key already exists.

removeItem(keyname)

Removes the specified key from the storage.

clear()

Clears all keys from the storage.

Related Pages for Web Storage API

Property

Description

window.localStorage

Allows saving key/value pairs in a web browser, with the data stored indefinitely and no expiration date.

window.sessionStorage

Allows saving key/value pairs in a web browser, with the data stored for the duration of a single session.