Is there something like isset php in javascript / jQuery?

Is there something in javascript / jQuery to check if a variable is set or inaccessible? In php, we use isset($variable) to check for something like this.

thank.

+57
javascript jquery php isset
Nov 20 '10 at 8:01
source share
11 answers

Try the following expression:

 typeof(variable) != "undefined" && variable !== null 

This will be true if the variable is defined and not null, which is equivalent to how PHP isset works.

You can use it as follows:

 if(typeof(variable) != "undefined" && variable !== null) { bla(); } 
+111
Nov 20 '10 at 8:07
source share

JavaScript isset () in PHP JS

 function isset () { // 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; } 
+8
Apr 13 '12 at 9:00
source share

typeof will serve the purpose, I think

 if(typeof foo != "undefined"){} 
+3
Nov 20 2018-10-10T00:
source share

If you want to check if the property exists: hasOwnProperty is the way to go

And since most objects are properties of some other object (ultimately resulting in a window object), this may work well to check if values ​​have been declared.

+3
Nov 20 '10 at 8:18
source share

Not naturally, no ... However, a google search: http://phpjs.org/functions/isset:454

+2
Nov 20 2018-10-10T00:
source share

http://phpjs.org/functions/isset:454

The phpjs project is a reliable source. A lot of js-equivalent php functions are available there. I have been using it for a long time and have not found any problems.

+2
Nov 20 2018-10-10T00:
source share

The problem is that passing the variable undefined to the function causes an error.

This means that you need to run typeof before passing it as an argument.

The cleanest way I found for this is as follows:

 function isset(v){ if(v === 'undefined'){ return false; } return true; } 

Using:

 if(isset(typeof(varname))){ alert('is set'); } else { alert('not set'); } 

Now the code is much more compact and readable.

This will still give an error if you try to call a variable from a non-specific variable, for example:

 isset(typeof(undefVar.subkey)) 

before trying to accomplish this, you need to make sure that the object is defined:

 undefVar = isset(typeof(undefVar))?undefVar:{}; 
+1
Dec 19 '13 at 8:23
source share

Here:)

 function isSet(iVal){ return (iVal!=="" && iVal!=null && iVal!==undefined && typeof(iVal) != "undefined") ? 1 : 0; } // Returns 1 if set, 0 false 
0
Apr 21 '14 at 17:19
source share

in addition to @ emil-vikström's answer , checking for variable!=null will be true for variable!==null as well as for variable!==undefined (or typeof(variable)!="undefined" ).

0
Jun 15 '14 at 16:39
source share

Some parts of each of these answers work. I compiled them all into the "isset" function, just as the question was asked and works like in PHP.

 // isset helper function var isset = function(variable){ return typeof(variable) !== "undefined" && variable !== null && variable !== ''; } 

Here is an example of how to use it:

 var example = 'this is an example'; if(isset(example)){ console.log('the example variable has a value set'); } 

It depends on the situation you need, but let me break down what each part does:

  • typeof(variable) !== "undefined" checks if a variable is defined at all
  • variable !== null checks if the variable is null (some explicitly set the value to null and do not think that it is set to zero, that it is correct, in this case delete this part)
  • variable !== '' checks if the variable is set to an empty string, you can delete it if the empty string is counted as set for your use.

Hope this helps someone :)

0
Dec 09 '15 at 17:10
source share

You can simply:

 if(variable||variable===0){ //Yes it is set //do something } else { //No it is not set //Or its null //do something else } 
-one
Mar 05 '14 at 9:20
source share



All Articles