1

How would i do the below with Javascript?

var object

function() {
    return {
        object: Return true if object exists or object is set to true, or false if set to false, if object doesnt exisit return false
    }
}
Kingy04
  • 93
  • 1
  • 8

4 Answers4

1

Objects that do not exist are undefined. You can compare object with undefined to check for its existence.

Make sure to use === to check equality with types.

E Rullmann
  • 344
  • 3
  • 12
0

You could use instanceof to check if its an object. Typeof would return true if the object is null, because null is technically an object!

var obj = {};
var obj2 = null;

alert("Is obj a true object? "+isObject(obj));
alert("Is obj2 a true object? "+isObject(obj2));

// False positive
alert("Is obj a object? "+isObjectFalsePositive(obj));
alert("Is obj2 a object? "+isObjectFalsePositive(obj2));

function isObject(inputVar) {
   return inputVar instanceof Object;
}

function isObjectFalsePositive(inputVar) {
   return typeof inputVar === 'object';
}

Ex: https://jsfiddle.net/2Lt2L14b/5/

dotKn0ck
  • 196
  • 1
  • 1
  • 12
0

You need to check for undefined and false to return value as false. If the object value is set to true the return flag should be true. Below code snippet might help you.

var object;
function checkObject() {
 var returnFlag;
 if(typeof object == 'undefined' || !object) {
  returnFlag = false;
 } else if(object) {
  returnFlag = true;
 }
 return returnFlag;
}

console.log(checkObject());

Or you can just return !!object

var object;
function checkObject() {
 return !!object;
}

console.log(checkObject());
kk.
  • 3,747
  • 12
  • 36
  • 67
  • 1
    Should be `typeof object == 'undefined'` – Barmar Jun 23 '17 at 20:56
  • Thanks for the answers so far however, I was hoping for a solution in one line using shorthand, that takes into account, if object = true or false or if object is simply present its true, if not present it should be false – Kingy04 Jun 24 '17 at 09:37
  • My 2nd solution is one liner – kk. Jun 24 '17 at 09:39
  • You can remove the assignment and check it.. this will return false. I have updated my answer for your test. – kk. Jun 24 '17 at 13:15
  • in this instance i need it to return true, thats the problem :) – Kingy04 Jun 24 '17 at 13:51
  • what you are saying, that is impractical and impossible. Javascript will not execute at all. – kk. Jun 24 '17 at 13:56
-4

return yourElement.length > 0;

chopper
  • 6,649
  • 7
  • 36
  • 53
Yovav
  • 2,557
  • 2
  • 32
  • 53
  • 4
    This will get an error if the object doesn't exist. – Barmar Jun 23 '17 at 20:53
  • No it wont, try it, in JavaScript its a good way to check if some element exist or not... – Yovav Jun 23 '17 at 20:55
  • 3
    tried it... `console.log(bladibla.length)` .... Uncaught ReferenceError: bladibla is not defined... of course Barmar doesn't know what he's talking about ;) =) – yezzz Jun 23 '17 at 21:00
  • I wasn't clear enough - what I meant is that yourElement is defined in jQuery as: $("#YourElementID").length > 0 (that will work) – Yovav Jun 24 '17 at 16:56