JavaScript isset() equivalent

JavaScript isset() equivalent

In PHP you can do if(isset($array[‘foo’])) { … }. In JavaScript you often use if( { … } to do the same, but this is not exactly the same statement. The condition will also evaluate to false if does exists but is false or 0 (and probably other values as well).
What is the perfect equivalent of PHP’s isset in JavaScript?
In a broader sense, a general, complete guide on JavaScript’s handling of variables that don’t exist, variables without a value, etc. would be convenient.


Solution 1:

I generally use the typeof operator:

if (typeof !== 'undefined') {
  // your code here

It will return "undefined" either if the property doesn’t exist or its value is undefined.

(See also: Difference between undefined and not being defined.)

There are other ways to figure out if a property exists on an object, like the hasOwnProperty method:

if (obj.hasOwnProperty('foo')) {
  // your code here

And the in operator:

if ('foo' in obj) {
  // your code here

The difference between the last two is that the hasOwnProperty method will check if the property exist physically on the object (the property is not inherited).

The in operator will check on all the properties reachable up in the prototype chain, e.g.:

var obj = { foo: 'bar'};

obj.hasOwnProperty('foo'); // true
obj.hasOwnProperty('toString'); // false
'toString' in obj; // true

As you can see, hasOwnProperty returns false and the in operator returns true when checking the toString method, this method is defined up in the prototype chain, because obj inherits form Object.prototype.

Solution 2:

Age old thread, but here’s a new way to run an equivalent isset().


See below for explanation. Note I use StandardJS syntax

Example Usage

// IMPORTANT pass a function to our isset() that returns the value we're
// trying to test(ES6 arrow function)
isset(() => some) // false

// Defining objects
let some = { nested: { value: 'hello' } }

// More tests that never throw an error
isset(() => some) // true
isset(() => some.nested) // true
isset(() => some.nested.value) // true
isset(() => some.nested.deeper.value) // false

// Less compact but still viable except when trying to use `this` context
isset(function () { return some.nested.deeper.value }) // false

Answer Function

 * Checks to see if a value is set.
 * @param {Function} accessor Function that returns our value
function isset (accessor) {
  try {
    // Note we're seeing if the returned value of our function is not
    // undefined
    return typeof accessor() !== 'undefined'
  } catch (e) {
    // And we're able to catch the Error it would normally throw for
    // referencing a property of undefined
    return false



Note that in PHP you can reference any variable at any depth – even trying to
access a non-array as an array will return a simple true or false:

// Referencing an undeclared variable
isset($some); // false

$some = 'hello';

// Declared but has no depth(not an array)
isset($some); // true
isset($some['nested']); // false

$some = ['nested' => 'hello'];

// Declared as an array but not with the depth we're testing for
isset($some['nested']); // true
isset($some['nested']['deeper']); // false


In JavaScript, we don’t have that freedom, we’ll always get an error if we do
the same because JS is immediately attempting to access the value of deeper
before we can wrap it in our isset() function so…

// Common pitfall answer(ES6 arrow function)
const isset = (ref) => typeof ref !== 'undefined'

// Same as above
function isset (ref) { return typeof ref !== 'undefined' }

// Referencing an undeclared variable will throw an error, so no luck here
isset(some) // Error: some is not defined

// Defining a simple object with no properties - so we aren't defining
// the property `nested`
let some = {}

// Simple checking if we have a declared variable
isset(some) // true

// Now trying to see if we have a top level property, still valid
isset(some.nested) // false

// But here is where things fall apart: trying to access a deep property
// of a complex object; it will throw an error
isset(some.nested.deeper) // Error: Cannot read property 'deeper' of undefined
//         ^^^^^^ undefined

More failing alternatives:

// Any way we attempt to access the `deeper` property of `nested` will
// throw an error
some.nested.deeper.hasOwnProperty('value') // Error
//   ^^^^^^ undefined

Object.hasOwnProperty('value', some.nested.deeper) // Error
//                                  ^^^^^^ undefined

// Same goes for typeof
typeof some.nested.deeper !== 'undefined' // Error
//          ^^^^^^ undefined

And some working alternatives that can get redundant fast:

// Wrap everything in try...catch
try { isset(some.nested.deeper) } catch (e) {}
try { typeof some.nested.deeper !== 'undefined' } catch (e) {}

// Or by chaining all of the isset which can get long
isset(some) && isset(some.nested) && isset(some.nested.deeper) // false
//                        ^^^^^^ returns false so the next isset() is never run


All of the other answers – though most are viable…

  1. Assume you’re only checking to see if the variable is not undefined which
    is fine for some use cases but can still throw an Error
  2. Assume you’re only trying to access a top level property, which again is
    fine for some use cases
  3. Force you to use a less than ideal approach relative to PHP’s isset()
    e.g. isset(some, 'nested.deeper.value')
  4. Use eval() which works but I personally avoid

I think I covered a lot of it. There are some points I make in my answer that I
don’t touch upon because they – although relevant – are not part of the
question. If need be, though, I can update my answer with links to some of the
more technical aspects based on demand.

I spent waaay to much time on this so hopefully it helps people out.

Thank-you for reading!

Solution 3:

Reference to SOURCE

    module.exports = function isset () {
  //  discuss at:
  // original by: Kevin van Zonneveld (
  // 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
  var l = a.length
  var i = 0
  var undef

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

  while (i !== l) {
    if (a[i] === undef || a[i] === null) {
      return false

  return true
} is mostly retired in favor of locutus
Here is the new link

Solution 4:

if (!('foo' in obj)) {
  // not set.

Solution 5:

//  tring to reference non-existing variable throws ReferenceError 
//  before test function is even executed
//  example, if you do:
//     if ( isset( someVar ) ) 
//        doStuff( someVar );
//  you get a ReferenceError ( if there is no someVar... ) 
//  and isset fn doesn't get executed.
//  if you pass variable name as string, ex. isset( 'novar' );, 
//  this might work:
function isset ( strVariableName ) { 

    try { 
        eval( strVariableName );
    } catch( err ) { 
        if ( err instanceof ReferenceError ) 
           return false;

    return true;


Solution 6:

This simple solution works, but not for deep object check.

function isset(str) {
    return window[str] !== undefined;