Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6021803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:45:58+00:00 2026-05-23T03:45:58+00:00

OK, so I have created an HTML5 canvas game that uses localStorage. I have

  • 0

OK, so I have created an HTML5 canvas game that uses localStorage. I have localStorage working perfectly but I’m not sure that it follows the correct standards, or if it is completely fine the way that I have written it.

//check if storage is set already, if not set the variable   
function checkLocalStorage(){  
if (localStorage.timesPlayed){  
timesPlayed = Number(localStorage.timesPlayed);  
}   
else{   
timesPlayed = 0;   
}   
}

//code snippet to increase my variable   
timesPlayed += 1;

//code snippet to set the localStorage   
localStorage.timesPlayed = timesPlayed;

This works perfectly!? But from what I have read, i should be using localStorage.getItem and localStorage.setItem?

Should I change the way I write localStorage??

This is just the link to the website where I learned this way to access localStorage
http://hacks.mozilla.org/2009/06/localstorage/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:45:59+00:00Added an answer on May 23, 2026 at 3:45 am

    It works, and it probably won’t break, but I’d still try to cast things in the correct type when using localStorage. getItem and setItem are the preferred ways of doing things, but the thing that jumped out at me was that your methods of getting and setting the value won’t work for any type but a number, which means you have to code cautiously if you’re using a lot of localStorage.

    You’re sending a number to localStorage, where it’s converted to a string. This can get you into a lot of trouble when dealing with booleans, for example:

    var won = false;
    localStorage.isWinner = won;
    if (localStorage.isWinner) { // always true
      alert("You won!");
    }
    

    Arrays and objects get ugly too:

    localStorage.test = {foo: "bar", dog: "cat"};
    // returns "[object Object]"
    

    In other words, what you have works, but it’s a good habit to do things correctly and consistently from the start, as it will simplify debugging later. The correct way — which works with strings, numbers, arrays, objects — is this:

    localStorage.setItem("key", JSON.stringify(value));
    JSON.parse(localStorage.getItem("key"));
    
    // or as a de facto alternative
    
    localStorage.key = JSON.stringify(value);
    JSON.parse(localStorage.key);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an upcoming project that I would like to use the HTML5 canvas
I'm developing a painter program using HTML5 canvas. I have created a drawing tool
I have created an image in my Canvas with HTML5 and I saved it.
I'm working on a simple JavaScript game using HTML5 canvas. It's a very typical
I have HTML5 canvas where the user has created an image. This is for
I have created a html page in php and upon submission i validates that
Come see the amazing disappearing rectangle! But seriously I have a really simple HTML5
I have this script I am working on that utilizes the oCanvas JS Library
With the beginning of widespread HTML5 support and Canvas support, it seems that someone
I've created a drawing of a truck using paths in an HTML5 canvas. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.