is there something like isset of php in javascript/jQuery?

JavascriptPhpJqueryIsset

Javascript Problem Overview


Is there something in javascript/jQuery to check whether variable is set/available or not? In php, we use isset($variable) to check something like this.

thanks.

Javascript Solutions


Solution 1 - Javascript

Try this expression:

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

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

You can use it like this:

if(typeof(variable) != "undefined" && variable !== null) {
    bla();
}

Solution 2 - Javascript

JavaScript isset() on 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;
}

Solution 3 - Javascript

typeof will serve the purpose I think

if(typeof foo != "undefined"){}

Solution 4 - Javascript

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

And since most objects are properties of some other object (eventually leading to the window object) this can work well for checking if values have been declared.

Solution 5 - Javascript

Not naturally, no... However, a googling of the thing gave this: http://phpjs.org/functions/isset:454

Solution 6 - Javascript

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

phpjs project is a trusted source. Lots of js equivalent php functions available there. I have been using since a long time and found no issues so far.

Solution 7 - Javascript

Some parts of each of these answers work. I compiled them all down into a function "isset" just like the question was asking and works like it does in PHP.

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

Here is a usage 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 it for but let me break down what each part does:

  1. typeof(variable) !== "undefined" checks if the variable is defined at all
  2. variable !== null checks if the variable is null (some people explicitly set null and don't think if it is set to null that that is correct, in that case, remove this part)
  3. variable !== '' checks if the variable is set to an empty string, you can remove this if an empty string counts as set for your use case

Hope this helps someone :)

Solution 8 - Javascript

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

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

The cleanest way I found to do this is like so:

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

Usage:

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 instantiated variable like:

isset(typeof(undefVar.subkey))

thus before trying to run this you need to make sure the object is defined:

undefVar = isset(typeof(undefVar))?undefVar:{};

Solution 9 - Javascript

Here :)

function isSet(iVal){
 return (iVal!=="" && iVal!=null && iVal!==undefined && typeof(iVal) != "undefined") ? 1 : 0;
} // Returns 1 if set, 0 false

Solution 10 - Javascript

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

Solution 11 - Javascript

You can just:

if(variable||variable===0){
    //Yes it is set
    //do something
}
else {
    //No it is not set
    //Or its null
    //do something else 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongautamlakumView Question on Stackoverflow
Solution 1 - JavascriptEmil VikströmView Answer on Stackoverflow
Solution 2 - JavascriptOlegView Answer on Stackoverflow
Solution 3 - JavascriptDanneManneView Answer on Stackoverflow
Solution 4 - JavascriptzzzzBovView Answer on Stackoverflow
Solution 5 - JavascriptIoannis KaradimasView Answer on Stackoverflow
Solution 6 - JavascriptSandeepan NathView Answer on Stackoverflow
Solution 7 - JavascriptstuyamView Answer on Stackoverflow
Solution 8 - JavascriptDieter GribnitzView Answer on Stackoverflow
Solution 9 - JavascriptMr BillView Answer on Stackoverflow
Solution 10 - Javascriptuser2619604View Answer on Stackoverflow
Solution 11 - Javascripta1204773View Answer on Stackoverflow