Storage.setItem method

  • The setItem method is used to store a key-value pair.

  • If the key already exists, its value is updated; otherwise, a new key-value pair is created.

  • The example shows how to save the number of stars earned in a game using setItem.

setItem(
  key: string,
  value: string
): void;

Sets the value of the pair identified by key to value, creating a new key-value pair if none existed for key previously.

Examples

// User earns a new star.
starsEarned++;

GameSnacks.storage.setItem('starsEarned', starsEarned.toString());