JavaScript isset() equivalent

JavaScript isset() equivalent

I generally use the typeof operator:

if (typeof obj.foo !== undefined) {
  // your code here
}

It will return undefined either if the property doesnt 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.

Age old thread, but there are new ways to run an equivalent isset().

ESNext (Stage 4 December 2019)

Two new syntax allow us to vastly simplify the use of isset() functionality:

Please read the docs and mind the browser compatibility.

Answer

See below for explanation. Note I use StandardJS syntax

Example Usage

// IMPORTANT pass a function to our isset() that returns the value were
// 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
 * @returns {Boolean}           Value is not undefined or null
 */
function isset (accessor) {
  try {
    // Note were seeing if the returned value of our function is not
    // undefined or null
    return accessor() !== undefined && accessor() !== null
  } catch (e) {
    // And were able to catch the Error it would normally throw for
    // referencing a property of undefined
    return false
  }
}

NPM Package

This answer function is available as the isset-php package on NPM. The package contains a few improvements such as type checking and supporting multiple arguments.

npm install --save isset-php

The full documentation is available in the README.

const isset = require(isset-php)
let val = 

// This will evaluate to true so the text will be printed.
if (isset(() => val)) {
  console.log(This val is set so I will print.)
}

Explanation

PHP

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 were testing for
isset($some[nested]); // true
isset($some[nested][deeper]); // false

JavaScript

In JavaScript, we dont have that freedom; well always get an error if we do
the same because the engine 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 arent 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

// Similar to the above but safe from objects overriding `hasOwnProperty`
Object.prototype.hasOwnProperty.call(some.nested.deeper, value) // 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 {
  if (isset(some.nested.deeper)) {
    // ...
  }
} catch (e) {}

try {
  if (some.nested.deeper !== undefined && some.nested.deeper !== null) {
    // ...
  }
} 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

Conclusion

All of the other answers – though most are viable…

  1. Assume youre 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 youre 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 PHPs 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
dont touch upon because they – although relevant – are not part of the
question(e.g. short circuiting). 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!

JavaScript isset() equivalent

Reference to SOURCE

    module.exports = function isset () {
  //  discuss at: http://locutus.io/php/isset/
  // original by: Kevin van Zonneveld (http://kvz.io)
  // improved by: FremyCompany
  // improved by: Onno Marsman (https://twitter.com/onnomarsman)
  // improved by: RafaƂ Kukawski (http://blog.kukawski.pl)
  //   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
    }
    i++
  }

  return true
}

phpjs.org is mostly retired in favor of locutus
Here is the new link http://locutus.io/php/var/isset

Leave a Reply

Your email address will not be published.