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 7974353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:20:39+00:00 2026-06-04T08:20:39+00:00

I need to store an object in localStorage – and I know that in

  • 0

I need to store an object in localStorage – and I know that in order to do so, I have to convert the object into a string. All cool.

My problem is in actually creating the object in the first place: I have two values in sessionStorage that need to be added to the object which is then passed into localStorage. However, when I try to create the object, one value is being stored as the variable name rather than its (numeric) value. Any idea whats going on here?

var siteName = sessionStorage['1'];
var siteID = (+sessionStorage['2']);
var temp = {siteID:siteName};
alert(typeof siteID);
alert(JSON.stringify(temp));

The first alert confirms that siteID is indeed a number type, but the second alert shows that the variable name (siteID) is stored rather than its numeric value.

  • 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-06-04T08:20:41+00:00Added an answer on June 4, 2026 at 8:20 am

    This line:

    var temp = {siteID:siteName};
    

    …creates an object containing a property called siteId with the value taken from the siteName variable.

    If you want the property name to be taken from the siteID variable instead:

    var temp = {};
    temp[siteID] = siteName;
    

    Or in ES2015 (aka “ES6”) you could use the new computed property name syntax:

    // ES2015+ only!
    var temp = {[siteId]: siteName};
    

    In JavaScript, you can access/create properties on objects in two different but equal ways: Using dotted notation with a literal property name:

    obj.foo = "bar";    // Creates a `foo` property on `obj` with the value `"bar"`
    

    …or using bracketed notation and a string:

    obj["foo"] = "bar"; // Does the same thing
    

    The keys in object initializers like your var temp = {siteID:siteName}; are always used literally (although they can optionally be in quotes); there’s no way with an object initializer to have a key taken from a variable instead. So you have to do it as a two-step process, first create the object, then set the property.

    So, if you do

    temp[siteID] = siteName;
    

    …the number in siteID will be converted to a string and will become the property name, with the value of siteName being the value.

    var temp = {};
    var key = 1;
    temp[key] = "value";
    console.log(temp[1]); // "value"
    console.log(temp["1"]); // "value"
    

    (Property names are always strings in JavaScript [for now].)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program in which I need to store a Class object into
I´ve a Javascript object with all the data that I need to store on
I need to store this object into the internal storage memory of the phone,
I need to create a class that will store a unique object elements. I
I need to store in my object which values have already been handlerd, I
I have a need to store multiple datatypes(like int or string mostly) inside a
I need to store a Calendar object as a String and also be able
I need to store a JSONArray object as a string then retrieve the string
I need to store the user who is creating a specific object in the
What i need to do, is to store a preference stucture in an object.

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.