8

How to check isset in javascript.

I have used in the following way.

var sessionvalue = document.getElementById('sessionvalue').value;

if(Here I have to check if isset the sessionvalue or not){

  if(sessionvalue == "" || sessionvalue == null)
    {
        document.getElementById('sessionvalue').style.borderColor="red";
        return false;
    }
    else
    {
        document.getElementById('sessionvalue').style.borderColor="#ccc";
    }
}
Arulkumar
  • 12,153
  • 12
  • 44
  • 61
dhiv
  • 111
  • 1
  • 1
  • 6
  • similar but def not a duplicate question.. guy's asking about a variable, the referenced question is asking about array values. This is a good question. – Robert Sinclair Jul 03 '19 at 05:11

5 Answers5

19

When javascript variables are not declared and you try to call them, they return undefined, so you can do:

if (typeof sessionvalue == "undefined" || sessionvalue == null)

AyB
  • 11,136
  • 4
  • 29
  • 46
  • 1
    and yet `if (typeof(sessionvalue.property) == "undefined" || sessionvalue.property == null)` results in the exception `Uncaught ReferenceError: sessionvalue is not defined` so only the tip of the ice-berg when it comes to implementing something even close to `isset`. down-voting accordingly. – user3338098 May 10 '16 at 17:16
  • @user3338098 The question was about if the object exists and not the object's property. Quite obviously, in order for the property of an object to exist, the object must exist first. So here your condition will slightly change. You will check if the object exists first and if it does, go on looking for the property. Refer an example here: https://jsfiddle.net/kx6gcLLm/ – AyB May 11 '16 at 05:35
  • either way, an equivalent to the *php* `isset` function, which checks any number of properties, is quite different in nature. consider: php: `isset(a->b->c->d->e->f)` and now *javascript* `if (typeof a == "undefined" || a == null || typeof(a.b) == "undefined" || a.b == null || typeof(a.b.c) == "undefined" || a.b.c == null || typeof(a.b.c.d) == "undefined" || a.b.c.d == null || typeof(a.b.c.e) == "undefined" || a.b.c.e == null || typeof(a.b.c.e.f) == "undefined" || a.b.c.e.f == null)` now can we see how the difference is so extreme as to be useless. – user3338098 May 11 '16 at 15:33
15

You can just do:

if(sessionvalue)

The above will automatically check for undefined, null (And NaN ,false,"")

You can even make it a global function if you need it like you're used to in php.

function isset(_var){
     return !!_var; // converting to boolean.
}
Amit Joki
  • 53,955
  • 7
  • 67
  • 89
2
    if(typeof(data.length) != 'undefined')
    {
       // do something
    }

    if(empty(data))
    {
        // do something
    }

   if(typeof(data) == 'undefined' || data === null)
   {
     //do something
   }
Puzzled Boy
  • 2,644
  • 7
  • 38
  • 74
0

you can just do if(sessionvalue) that's it you don't need anything else and remember you can compare apples with cars in javascript, you can check if value is null or undefined with if(sessionvalue) or if(!sessionvalue), your code will be :

document.getElementById('sessionvalue').style.borderColor= sessionvalue ? "red" : "#CCC";
Labib Ismaiel
  • 1,220
  • 2
  • 10
  • 21
0

Try Code as below

var sessionvalue=document.getElementById('sessionvalue').value;
if(typeof sessionvalue != 'undefined'){

    if(sessionvalue=="" || sessionvalue == null)
    {
        document.getElementById('sessionvalue').style.borderColor="red";
        return false;
    }
    else
    {
        document.getElementById('sessionvalue').style.borderColor="#ccc";


    }
}
Gopal Joshi
  • 2,284
  • 19
  • 47