Checking undefined value not working?

javascript check if not undefined
javascript check undefined or null
how to check undefined in jquery
check if undefined javascript
test if value is undefined
javascript object property exists but is undefined
javascript undefined error
how to handle undefined in javascript

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.

How can I determine if a variable is 'undefined' or 'null'?, Use try / catch if you are working with input you don't have control over. You should not have any references to undeclared variables in your code. (Note that this also checks if the variable contains any usable values. But since this� Above, undefined is a parameter whose value is undefined, because it has not been provided by the caller. Technique 2: compare with void 0 . The void operator [2] evaluates its operand, ignores the result and returns undefined .

I would use a direct comparison instead of the notoriously odd "typeof" operator:

if ((currentIds === undefined) || (currentIds === null)) {
  //...

How to Determine If Variable is Undefined or NULL in JavaScript, If you try to test the null values using the typeof operator it will not work as expected, because JavaScript return "object" for typeof null instead of "null". This is a� How to check if a JavaScript object property is undefined In a JavaScript program, the correct way to check if an object property is undefined is to use the `typeof` operator. See how you can use it with this simple explanation. Published May 26, 2018, Last Updated May 29, 2019

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);

undefined, If the value is not defined, typeof returns the 'undefined' string. Now suppose you have a car Duration: 1:46 Posted: May 26, 2018 Within jQuery, foo === undefined is checking against the local copy of undefined instead of the global (window.undefined), which may have been modified by insane code. The fact that undefined is mutable is definitely worth noting and I'm glad you did.

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.

How to check if a JavaScript object property is undefined, “Undefined is supposed to mean a variable has no value (or a property does not exist) because the programmer has not yet assigned it a value (� If this is declared but not defined, the console.log() will return "Incoming value is: undefined". Note we are not using the strict equals operator. Note we are not using the strict equals operator. The "correct" way (check the other answers for details), if the incomingValue is not a boolean type, just evaluate if its value is true, this will


[Edit Edit Edit Edit :P]


currentIds = "undefined"

implies

typeof currentIds == "String"

Also see, Detecting Undefined, === isn't necessary for string comparison.

How to Check for Undefined in JavaScript, Undefined value primitive value is used when a variable has not been Let's check the function that verifies whether a word is a palindrome: var declaration problem is the variable hoisting in the entire function scope. As a result, JSON-based databases may support null values but not undefined values. Since attributes set to null are encoded, you can transmit the intent to clear an attribute by setting its value to null before encoding and transmitting the object to a remote store.

7 Tips to Handle undefined in JavaScript, if (typeof x === "undefined") { The undefined property indicates that a variable has not been assigned a value, Test if a not declared variable is undefined:. Few months ago one developer send me email and he say me to i am working on my angularjs application and i need to check variable value is empty or null then both how can i do it. he also send me some code that he tried out. actually his code is also was working but i said him this is not way to you are checking object is empty, null or

JavaScript undefined Property, Check by Value (Strict equality Operator): Here you will get that variable is assigned a value or not if the variable is not assigned a value it will display undefined. By specification, the built-in toString can be extracted from the object and executed in the context of any other value. And its result depends on that value. For a number, it will be [object Number] For a boolean, it will be [object Boolean] For null: [object Null] For undefined: [object Undefined] For arrays: [object Array] …etc (customizable).

variable === undefined vs. typeof variable === “undefined” in , You do it exactly the same way you'd do it without jQuery: [code js] function isUndefined(x) { return typeof x == "undefined"; } [/code] The main difference is that undefined represents the value of a variable that wasn’t yet initialized, while null represents an intentional absence of an object. Let’s explore the difference in some examples. The variable number is defined, however, is not assigned with an initial value:

Comments
  • Well that must mean that the value of currentIds is not really undefined.
  • @Pointy: it's undefined. The statement $.myNameSpace.currentIDs = currentIds.Split(','); throws an exception.
  • Is currentIds "undefined" or is it undefined? typeof "undefined" is string.
  • currentIds hasn't got any value.
  • Either you are stuck in an alternate universe, or the value of currentIds isn't what you think it is.
  • This is wrong, localStorage.getItem() does not return the string "undefined", it returns null, you only need the null check, not the other stuff that you're blindly testing.
  • @JuanMendes, i got an undefined and this syntax worked for my problem. In fact, it was the odd == "undefined" that caught my problem!
  • @DaveA That is because you saved something that was undefined, and local storage converts it into the string "undefined". When saving something into localStorage, you should be careful not to set it to undefined, or null. I would always set it to the empty string instead
  • @JuanMendes, i did indeed save invalid data. App is in early development and sloppy things happen. But I don't think the string "undefined" went into memory. But things like this will happen. You can kill yourself with validation, but issues will crawl past you...
  • @DaveA localStorage.setItem('test', undefined) will cause localStorage.getItem('test') to return 'undefined', the string. That can easily happen if you set it to a falsy value . That's what you should avoid