6

I'm looking for the javascript equivalent of the php function isset(). I've tried the method described here at JavaScript isset() equivalent but at firebug, error comes up saying

data.del is undefined                          //Firebug warning/error
 if(typeof data.del[0].node != 'undefined') { // codes in my js file

And in some cases

data is null                                  //Firebug warning/error
  if(typeof data.storyLine != 'undefined') { // codes in my js file

The logic seems to work but I'm wondering why is there an error then??

Basically, I want to check whether data.del[0].node or data.storyLine isset or not??

Community
  • 1
  • 1
ptamzz
  • 9,235
  • 31
  • 91
  • 147
  • or you can use `hasOwnProperty` – KJYe.Name Mar 25 '11 at 19:12
  • please elaborate on what conditions will return a result of `true` for `isset()`, specifically: `NaN` and `undefined` – zzzzBov Mar 25 '11 at 19:23
  • @zzzzBov, it's jst i make a msql query, if data are there, i return them, using json_encode of an array.. if data are not there, nothing is returned. – ptamzz Mar 25 '11 at 19:29
  • then use `data.hasOwnProperty('foo');` – zzzzBov Mar 25 '11 at 19:33
  • @Jared Farrish, I distinctly remember watching a [lecture given by Douglas Crockford](http://developer.yahoo.com/yui/theater/) mentioning something about an issue with `in` but I believe it was in the context of a `for...in` loop. I believe `in` will work in most cases, but i'm more certain `hasOwnProperty` will work as expected. – zzzzBov Mar 25 '11 at 19:44
  • Possible duplicate of [JavaScript isset() equivalent](https://stackoverflow.com/questions/2281633/javascript-isset-equivalent) – Yep_It's_Me Oct 30 '18 at 00:53
  • Refer this for answer https://stackoverflow.com/questions/2281633/javascript-isset-equivalent/56887380#answer-56887380 – bikash.bilz Jul 04 '19 at 11:57

3 Answers3

11

isset() makes two checks: first if the variable is defined, and second if it is null.

You will have to check for both the 'undefined' case and the null case, for example:

if (typeof data !== 'undefined' && data !== null)
Bobby D
  • 2,129
  • 14
  • 21
5

ECMAScript defines the hasOwnProperty method for checking if an object has a property of a given name:

var foo = {'bar':'bar'}

alert( foo.hasOwnProperty( 'bar' ) ); //true
alert( foo.hasOwnProperty( 'baz' ) ); //false

EDIT: This doesn't fully answer your question

It's possible for a property to be set as undefined

foo.bar = undefined;

alert( foo.hasOwnProperty( 'bar' ) ); //still true

The important question is: What do you need your truth table to be?

In php:

type  | isset() | == true
------+---------+----------
null  | false   | false
false | true    | false
true  | true    | true
""    | true    | false
"a"   | true    | true
0     | true    | false
1     | true    | true

In JS:

type      | isset() | truthy
----------+---------+--------
NaN       | ?       | false
undefined | ?       | false
null      | false   | false
true      | true    | true
false     | true    | false
""        | true    | false
"a"       | true    | true
0         | true    | false
1         | true    | true
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • it's jst that, after making a mysql_query, if the data im seeking is in the database, im returning the data using json_encode() function of PHP. If the data is not there in the database, it's never returned. So I want to checked whether data did arrive from the database. If i got the data.. i do stuffs.. if i don't dont do stuffs.. that's like it. – ptamzz Mar 25 '11 at 19:33
  • it works... i've tried `if(typeof data.node != 'undefined')`, `if(data.node != 'undefined' && data.node != 'null')`, and `data.hasProperty('node')`.. all of them seems to work.. but all of them throws up this red error in firebug so i'm just worried and wondering if it's working by luck!! – ptamzz Mar 25 '11 at 19:48
  • @zzzzBov - Oops, sorry, I moved the comment because I didn't know if you'd see it. Here's a test that I wasn't confident enough to post as an answer, which uses `in`: http://jsfiddle.net/ZRnda/ Note: No Firebugz errors! (FF 4, FB 1.7) – Jared Farrish Mar 25 '11 at 19:51
  • @ptamzz, i assume you forgot the `typeof` in your second example. They're all valid ways of checking if a property was set, the only difference between them will be the edge cases. – zzzzBov Mar 25 '11 at 20:05
  • @Jared Farrish, `in` is a valid operation in JS, it won't throw an error, it just might behave unexpectedly in certain edge cases. – zzzzBov Mar 25 '11 at 20:06
4

I think the best solution is to look in the source code of php.js:

function isset () {
    // !No description available for isset. @php.js developers: Please update the function summary text file.
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/isset
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // +   improved by: Rafał Kukawski
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
    var a = arguments,
        l = a.length,
        i = 0,
        undef;

    if (l === 0) {
        throw new Error('Empty isset');
    }

    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false;
        }
        i++;
    }
    return true;
}
Harmen
  • 22,092
  • 4
  • 54
  • 76