Use PHP functions in JavaScript

JavaScript isset

!No description available for isset. @php.js developers: Please update the function summary text file.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
function isset () {
    // !No description available for isset. @php.js developers: Please update the function summary text file.
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/isset    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // *     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;
        if (l===0) {
        throw new Error('Empty isset'); 
    }
    
    while (i!==l) {        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }    }
    return true;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
isset( undefined, true);

Should return

1
false

» Example 2

Running

1
isset( 'Kevin van Zonneveld' );

Should return

1
true

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have isset goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
mk.keck
6 Mar '09 Permalink

q   Function isset() does not work?
I've done follow test:

1
2
3
4
5
if (!isset(a)) { // a does'nt really exists
    alert('Var \'a\' is undefined');
} else {
    alert('Var \'a\' = "' + a + '"');
}

I get an Error from browser 'a is undefined'.
If I use isset('a') I get alwasy true ... ?

I've changed the function isset():
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
function isset() {
    var a = arguments;
    if (a.length > 0) {
        var i = 0;
        while (i !== a.length) {            if (typeof(a[i]) === 'object') {
                if (typeof(a[i]) === 'undefined' || a[i] === null) {
                    return false;
                } else {
                    i++;                }
            } else if (typeof(a[i]) === 'string') {
                if (typeof(window[a[i]]) === 'undefined' || window[a[i]] === null) {
                    return false;
                } else {                    i++;
                }
            }
        }
        return true;    }
    return false;
}

Now I get the correct answer from function isset

Gravatar
Kevin van Zonneveld
1 Feb '09 Permalink

q  @ Maxim: :)

Gravatar
Maxim
27 Jan '09 Permalink

q  sorry, it doesn't work in Firebug console,
but works properly in firefox itself.

thanks for good function :)

Gravatar
Maxim
27 Jan '09 Permalink

q  

1
2
3
var a=1;
delete a;
isset(a);

throws:
ReferenceError: a is not defined

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @ Onno Marsman: Another great attribution to our project! Thank you so much!

Gravatar
Onno Marsman
11 Aug '08 Permalink

q   1. should return false also on null values like in php
2. should throw an error when no arguments are provided
3. should throw an error when passed arguments are not variables, like in both examples. They would give an error in PHP. Not sure if this is possible or needed by anyone.

Implementation for 1. and 2. :
[CODE ="Javascript"]
function isset() {
var a=arguments; var l=a.length; var i=0;
if (l==0) { throw new Error('Empty isset'); }
while (i!=l) {
if (typeof(a[i])=='undefined' || a[i]===null) { return false; } else { i++; }
}
return true;
}
[/CODE]
I made up an error message because I thought:

Parse error: syntax error, unexpected ')', expecting T_STRING or T_VARIABLE or '$'

Would be pushing it ;)

The check for l==0 could be placed after the while to make it a littlebit more efficient in the case it would return false, but I think this is more readable.

Gravatar
Kevin van Zonneveld
17 Apr '08 Permalink

q  @ FremyCompany: Updated, thank you!

Gravatar
[FremyCompany] Can be maked quicker :
16 Apr '08 Permalink

q  

1
2
3
4
56
7
function isset() {
  var a=arguments; var l=a.length; var i=0;
  while (i!=l) {
    if (typeof(a[i])=='undefined') { return false; } else { i++; }
  }  return true;
}


Contribute a New function