---
title: setItem
framework: tvmljs
role: symbol
role_heading: Instance Method
path: tvmljs/storage/1627302-setitem
---

# setItem

Associates the given data with the given key.

## Declaration

```data
void setItem(
    in String key, 
    in String data
);
```

```data
void setItem(
    DOMString key, 
    DOMString data
);
```

## Parameters

- `key`: A String object containing the key associated with the data to be saved.
- `data`: A String object containing the data to be stored.

## Discussion

Discussion The setItem function first checks whether the given key already exists in the storage. If it does not, then the key and associated value are stored. If the key already exists and the data in storage is different than the data being passed, the new data is saved to storage.

## See Also

### Accessing Key-Value Pair Information

- [clear](tvmljs/storage/1627404-clear.md)
- [getItem](tvmljs/storage/1627333-getitem.md)
- [key](tvmljs/storage/1627380-key.md)
- [length](tvmljs/storage/1627448-length.md)
- [removeItem](tvmljs/storage/1627332-removeitem.md)
