Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Session Variable

Hello,

I have a really quick question, someone was talking about a session variable on a post on facebook. So I tried to google it to see what a session variable is, I didn't find too much about it, Can someone explain me what a session variable is?

1 Answer

Hey Aurelio,

This w3schools article contains information about using session storage and local storage. Session storage is just for one session (until the browser closes) whereas local storage will be kept until the cache is wiped. Here is the article (session storage is mentioned in detail closer to the bottom of the page): http://www.w3schools.com/html/html5_webstorage.asp

Thank you Marcus for your quick response, I'll have a look through the whole article, I hope this will make some things clear! :)

If you have any more questions about it, come back here, and I'll help you to the best of my ability! :)

Ok it did help me out quite a bit, but I guess those session variables aren't that much used compared to the localstorage, or am I wrong?

(I didn't even know about the localstorage before, it's really nice I get to know this now)

I would say it would be dependent on what you want to do with the code. If you only needed to grab a user's name from some JSON data and use it while their browsing the site, you could use sessionStorage to set and get the user's name from a nicely formatted location.

localStorage was a large part of my upgraded to-do list app because it persistently stores to-do list items. I haven't yet had a need for sessionStorage, but a lack of need does not necessarily mean a lack of total usage. You should just adapt the code to what you need.

Ok thanks alot Marcus, helped me alot!

Another thing to do when using storage is to always test for it like so:

if (window.localStorage) {
//do local storage getting/setting
}
//Or when using session storage
if (window.sessionStorage) {
//do session storage getting/setting
}

Don't make an application entirely dependent upon it, because although most common browsers have been utilizing HTML storage for a while now, there are still people that use older versions of browsers.

Oh, and no problem at all, Aurelio! Good luck in your projects!