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; } |
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.
sorry, it doesn't work in Firebug console,
but works properly in firefox itself.
thanks for good function :)
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.


mk.keck
6 Mar '09
I've done follow test:
1 2 3 4 5I get an Error from browser 'a is undefined'.
If I use isset('a') I get alwasy true ... ?
I've changed the function isset():
Now I get the correct answer from function isset