check for undefined or null - javascript

I'm checking if the value of cookie is undefined or null but weirdly this does not really work
Here's what I tried
function check(val){
if($.cookie(val) === "undefined" || $.cookie(val) === "null"){
return "1";
}
return $.cookie(val);
}
So even if my cookie has value null it would print out null instead of 1

That is because you are using quotes. In other words, "undefined" === undefined is false.
if($.cookie(val) === undefined || $.cookie(val) === null){ //this would be more apropriate

Please check if jquery cookie library is included , this is the most general cause.

Related

Why checking undefined type instead of just value?

I got recently such legacy code:
if (typeof value != "undefined" && value.someOperation()) { }
I understand that its preventing undefined object issue, however isn't simple to do such thing:
if (value && value.someOperation()) { }
Is somewhere in deep javascript a hack or some situation that it would not work at all? There is no possible to get 0 or false instead of that object here. I wonder if I can change the first expression to the second one and I wouldn't break anything.
In this line
if (typeof value != "undefined" && value.someOperation()) { }
typeof value != "undefined" is evaluated to false if and only if value is either null or undefined. However, if the value is either 0 or false then it will evaluate to true, and hence move on to second condition value.someOperation().
There is no possible to get 0 or false instead of that object here.
If the value of value cannot be 0 or false, even then you might want to check value.someOperation before checking value.someOperation() since value.someOperation() it may give following error
TypeError: undefined is not a function
In fact, in both cases you might want to check if value.someOperation first before value.someOperation() i.e.
if (value && value.someOperation && value.someOperation()) { }
or
if (typeof value != "undefined" && value.someOperation && value.someOperation()) { }

How to easily check if undefined in javascript object?

Assuming I have a javascript object "myobject" that just contains an empty object "{}".
I ideally in my code I want to do the following:
if (theobject[keyvar][subkeyvar]) {
// do something
}
The issue now is that because keyvar and subkeyvar do not actually exist within the object, it fails and comains that the properties are undefined. What is the simplest/least lines of code/best way to be able to do have it just "know it is undefined" and continue to execute the //do something or not without crashing?
I dont want to get too carried away with checking like:
if( keyvar in theobject ) {
if(subkeyvar in theobject) {
if.....
}
}
if (typeof theobject[keyvar] !== "undefined" && typeof theobject[keyvar][subkeyvar] !== "undefined") {
// keyvar and subkeyvar defined
}
first we check if keyvar typeof is undefined and then if subkeyvar is undefined and if they are both defined typeof theobject[keyvar] !== "undefined" && typeof theobject[keyvar][subkeyvar] !== "undefined" is true.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
you can try this :
if (theobject[keyvar] && theobject[keyvar][subkeyvar]) {
// do something
}
If they both can possibly be undefined, you have to test one, then the other.
if (theobject[keyvar] && theobject[keyvar][subkeyvar]) {
This will of course fail on a key containing 0 or "" (or several other falsey but not undefined values) so you may want to use typeof on the second test.
Try:
if (theobject[keyvar] !== undefined && theobject[keyvar][subkeyvar] !== undefined) {
// do something
}
Since the && operator is used, the if will not check the second value if the first value is falsey, so you will never get the 'properties are undefined` error
theObject.hasOwnProperty(keyvar) && theObject.hasOwnProperty[keyvar].hasOwnProperty(subkeyvar)

javascript testing for object property

I have a string like this:
var TheDate = "6.14.2012";
and I have an object MyObject that has properties that may match that string. However, when I write this:
if (MyObject[TheDate] === null || MyObject[TheDate] === 'undefined') { .... }
the conditions never seem to evaluate to true even though I know the property doesn't exist.
What am I missing??
Thanks.
Existence of Own Property
If you want to check for the existence of a property use .hasownProperty()...
if (MyObject.hasOwnProperty(TheDate)) {
Existence of Own Or Inherited Property
If the property may be inherited, to test for existence use the in operator...
if (TheDate in MyObject) {
null or undefined value test for Own Or Inherited Property
If it's not existence, but rather a value test, and you want to test for null or undefined, then use this...
if (MyObject[TheDate] == null) {
This will check for both null and undefined.
The problem is with the quotes around undefined ;)
Change
if (MyObject[TheDate] === null || MyObject[TheDate] === 'undefined') { .... }
to
if (MyObject[TheDate] === null || MyObject[TheDate] === undefined) { .... }
It should work :)
If you want to use the string "undefined" rather than the undefined object, the way to do this is to check the "typeof", which returns a string. In this case, if you're looking to test if it's set to "undefined":
if ( MyObject[TheDate] === null || typeof MyObject[TheDate] === 'undefined' ) { .... }
Try the following
if ( (MyObject[TheDate] === null) || (typeof MyObject[TheDate] === 'undefined') {
....
}
You were checking whether your property contains the String 'undefined' and not whether its value is currently undefined.

Checking undefined value not working?

I have the following javascript code:
var currentIds = localStorage.getItem('currentPairsIds');
if ((typeof currentIds === "undefined") ||
(currentIds == null))
$.myNameSpace.currentIDs = new Array(3);
else
$.myNameSpace.currentIDs = currentIds.Split(',');
I'm debugging with Firebug and although currentIds hasn't got any value it always executes else statement.
UPDATE: I'm getting this value from HTML5 storage.
What am I doing wrong?
This is how I have solved my problem:
var currentIds = localStorage.getItem('currentPairsIds');
if ((currentIds === undefined) ||
(currentIds == null) || (currentIds == "undefined"))
$.myNameSpace.currentIDs = new Array(3);
else
$.myNameSpace.currentIDs = currentIds.split(',');
localStorage.getItem('currentPairsIds'); returns the string "undefined".
There is another error in Split() function. The right version is without any capital letter.
I would use a direct comparison instead of the notoriously odd "typeof" operator:
if ((currentIds === undefined) || (currentIds === null)) {
//...
It's not working because localStorage.getItem returns null if the item is not defined, it does not return undefined http://dev.w3.org/html5/webstorage/#dom-storage-getitem
Example: http://jsfiddle.net/mendesjuan/Rsu8N/1/
var notStored = localStorage.getItem('ffff');
alert(notStored); // null
alert(typeof notStored); // object, yes, null is an object.
Therefore you should just be testing
alert(notStored === null);
I think you have to make checking for undefined comparing with == instead of ===.
Example:
typeof currentIds == "undefined"
This will make sure, the variable is really undefined or not.
[Edit Edit Edit Edit :P]
currentIds = "undefined"
implies
typeof currentIds == "String"
Also see, Detecting Undefined, === isn't necessary for string comparison.
In my case LocalStorage.getItem() was converting it to "undefined" as string.
I also needed to check if it s "undefined" as a string.
var myItem = LocalStorage.getItem('myItem');
if(myItem != "undefined" && myItem != undefined && myItem != null){
...
}
if( typeof(varName) != "undefined" && varName !== null )
be sure, you use ( " " ) quotes when checking with typeof()

How to handle a not defined value in JavaScript

How to handle a not defined value in JavaScript?
if (oldins == ins)
oldins is not defined.
How do I check this?
if ((typeof(oldins) !== "undefined") && (oldins === ins))
Unset variables would evaluate to a value of 'undefined'. 'undefined' is a value type like null and NaN so it would be:
if ( typeof(oldins) == 'undefined' )
Edit: Fixed per comments. Leaving the answer since the comments are helpful, but there were more correct answers.
if (oldins !== undefined && oldins === ins) {
}

Categories

Resources