Adam Adam - 4 years ago 146
jQuery Question

jQuery: if condition is false, but still executed?

The following code


sessionStorage.test = false;

alert( sessionStorage.test );

if ( sessionStorage.test ) {
alert("I dont care about your conditions!");


produces the following popups:


I dont care about your conditions!

even though
is clearly set to false. Why is that the case? According to this answer, this should work. What am I doing wrong? I am using XAMPP - does it maybe have problems with sessionStorage? Here is the complete code of my test file:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<script src=""></script>
<script >
sessionStorage.test = false;
if ( sessionStorage.test ) {
alert("I dont care about your conditions!");
<!-- page content -->

Answer Source

The default method for sessionsStorage is:

sessionStorage.setItem('keyNameee', "valueeee");    

however, note that it wont help even. sessionStorage returns value, not conditional boolean. It's same if you set:


so, actual solution for you will be:

if (sessionStorage.getItem('test') != "false") {

Dublicate of Topics:
- sessionStorage setItem returns true or false
- sessionStorage isn't working as expected

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download