Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

p5.js | getItem() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The getItem() function is used to retrieve the value that has been stored using the storeItem() function under the given key name from the local storage of the browser. It returns a null value if the key is not found.

The local storage persists between browsing sessions and can store the values even after reloading the page unless it is cleared.



Parameters: This function accepts a single parameter as mentioned above and described below:

  • key: This is a string which denotes the key for which the value has to be retrieved.

Return Value: It returns the value of the stored item, which can be a String, Number, Boolean, Object, p5.Color or a p5.Vector.

Below example illustrates the getItem() function in p5.js:


function setup() {
  createCanvas(500, 300);
  text("Use the button to set and retrieve random values", 20, 20);
  getBtn = createButton('Get items from storage');
  getBtn.position(20, 150);
  setBtn = createButton('Set items to storage');
  setBtn.position(20, 180);
function retrieveStorage() {
  text("Use the button to set and retrieve random values", 20, 20);
  // retrieve values from local storage
  num = getItem('savedNumber');
  string = getItem('savedString');
  bool = getItem('savedBoolean');
  // display the values
  text("The retrieved items are:", 20, 40);
  text("Number: " + num, 20, 60);
  text("String: " + string, 20, 80);
  text("Boolean: " + bool, 20, 100);
function setStorage() {
  // generate random values
  randomNum = floor(random(100));
  randomStr = "Random String " + randomNum;
  randomBool = randomNum > 50 ? true : false;
  // store values to local storage
  storeItem('savedNumber', randomNum);
  storeItem('savedString', randomStr);
  storeItem('savedBoolean', randomBool);


Online editor:
Environment Setup:


My Personal Notes arrow_drop_up
Last Updated : 17 Jan, 2020
Like Article
Save Article
Similar Reads
Related Tutorials