Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Created August 26, 2017 22:57
Show Gist options
  • Save jendiamond/1ec51728b79792748c3312118eb554e3 to your computer and use it in GitHub Desktop.
Save jendiamond/1ec51728b79792748c3312118eb554e3 to your computer and use it in GitHub Desktop.

Checking for existance

If all these things are True Run the function

  if ((obj instanceof Object) &&
      (Array.isArray(obj[key])) &&
      (obj.hasOwnProperty(key)) &&
      (obj[key].length !== 0)) {

instanceof Object

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

typeof

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

if (obj instanceof Object && obj[key] instanceof Array && obj[key] != []) {};

Array.isArray()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

Array.isArray([1, 2, 3])
var animals = ['tapir','giraffe', 'ostrich']
Array.isArray(animals)

var a;
a = 123;
console.log(typeof a); // 'number'
a = "text string";
console.log(typeof a); // 'string'
a = [];
console.log(typeof a); // 'object'
a = {};
console.log(typeof a); // 'object'
a = true;
console.log(typeof a); // 'boolean'
a = null;
console.log(typeof a); // 'object'
a = undefined;
console.log(typeof a); // 'undefined'
a = function(){};
console.log(typeof a); // 'function'
a = new Date();
console.log(typeof a); // 'object'

https://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in

You can just check if the variable has a truthy value or not. That means

if( value ) {
}
will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

Other stuff

var obj = {
    first: "John", 
    last: "Doe"
};

//  Visit non-inherited enumerable keys

Object.keys(obj).forEach(function(key) {

    console.log(key, obj[key]);

});

JavaScript Properties

Properties are the values associated with a JavaScript object. A JavaScript object is a collection of unordered properties. Properties can usually be changed, added, and deleted, but some are read only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment