files/en-us/web/api/sharedstorage/set/index.md
{{APIRef("Shared Storage API")}}{{deprecated_header}}
The set() method of the {{domxref("SharedStorage")}} interface either stores a new key-value pair in the current origin's shared storage or updates an existing one.
set(key, value)
set(key, value, options)
key
value
options {{optional_inline}}
ignoreIfPresent
true causes the set operation to abort if a key-value pair with the specified key already exists. The default value false causes the set operation to overwrite the previous value.A {{jsxref("Promise")}} that fulfills with undefined.
Promise rejects with a {{jsxref("TypeError")}} if:
key and/or value exceed the browser-defined maximum length.Promise rejects with a {{jsxref("TypeError")}} if the worklet module has not been added with {{domxref("Worklet.addModule", "SharedStorageWorklet.addModule()")}}.[!NOTE] In the case of {{domxref("WindowSharedStorage")}}, if the
set()operation doesn't successfully write to the database for a reason other than shared storage not being available, no error is thrown — the operation still fulfills withundefined.
window.sharedStorage
.set("ab-testing-group", "0", {
ignoreIfPresent: true,
})
.then(() => console.log("Set operation completed"));
{{Specifications}}
{{Compat}}