Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Local Storage Project

Daniel Grieve
Daniel Grieve
6,432 Points

'localStorage.setItem' is not a function but 'sessionStorage.setItem' is?

My function for local storage is as follows...

function localStorage(ul){

        let str = JSON.stringify(ul.outerHTML)
        localStorage.setItem("rsvp", str) 
    }

For this I get the following error localStorage.setItem is not a function whereas if I change localStorage to sessionStorage it works as expected. Using console directly localStorage works no problem. What could be the reason for this?

1 Answer

Steven Parker
Steven Parker
229,657 Points

Your very first line is a re-definition of "localStorage" that replaces the built-in object (which does have a "setItem" method) with a new function which does not.

Give your function a different name to avoid this issue.