Dazak
Dazak

Reputation: 35

How to set up a variable = to a key in local storage

I try to create a variable with the key of my local storage as it value.

i tried let justId = localStorage.key

but the response i got in the console is just ƒ key() { [native code] } and i'm not able to use it on my code

Could you help me with that ? Thanks in advance

Upvotes: 2

Views: 2129

Answers (1)

Parvat R
Parvat R

Reputation: 846

To get and item or value from the localStorage, first you need to set a value.

localStorage.setItem('key', 'value');

// To get an item:
var result = localStorage.getItem('key');

The localStorage.key is used to get the key using the value

localStorage.setItem('name', 'My Name')

var myKey = localStorage.key('MyName')
console.log(myKey)
// logs 'name'

enter image description here

Upvotes: 2

Related Questions