Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

im trying to set x's age to the local storage item 'age' however, for a reason i do not know, this will not work.

Here is my code:

var x = {
age: 37,
gender: "male",
income: 17000,
localStorage.setItem("age") = x.age;
alert(localStorage.getItem('age'));

The problem is with your syntax. You need to use it this way.

localStorage.setItem("age", x.age)
alert(localStorage.getItem('age'));

The idea is simple. You are storing the data against a name. And then retrieve it using the same name.

Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present.

Then look at the manual which says:

storage.setItem(keyName, keyValue);

Then get your syntax correct:

var x = {
  age: 37,
  gender: "male",
  income: 17000,
localStorage.setItem("age", x.age);
alert(localStorage.getItem('age'));
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.