How to handle a not defined value in JavaScript - 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) {
}

Related

!fullResponse?.response?.is_complete does not act as it supposed to do

I am having an issue to understand this:
!fullResponse?.response?.is_complete
I was thinking it is the same as
fullResponse &&
fullResponse.response &&
'is_complete' in fullResponse.response &&
!fullResponse.response.is_complete
but it is not and it breaks my code specially when is_complete does not present in fullResponse.response
Can anyone explain what this does : !fullResponse?.response?.is_complete and if there is a way to make it act as below?
fullResponse &&
fullResponse.response &&
'is_complete' in fullResponse.response &&
!fullResponse.response.is_complete
The part you've probably misunderstood is the precedence of these operators. Your code actually boils down to:
!(
// vvvvvvv--- `== null`: either null or undefined
(fullResponse == null) ? undefined
:(fullResponse.response == null) ? undefined
:fullResponse.response.is_complete
)
So, when either part of your lookup short-circuits with an undefined, it runs right into the negation and gets converted to true.
If you just want to return false in case of a short-circuit, then it's as easy as:
// vvvv--- this will turn into false
!(fullResponse?.response?.is_complete ?? true)
However, if you want undefined in this case, it's easier with a variable:
const isComplete = fullResponse?.response?.is_complete
isComplete === undefined ? undefined : !isComplete
If you can't do that, you'll have to repeat the lookup:
fullResponse?.response?.is_complete === undefined ? undefined : !fullResponse?.response?.is_complete

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()) { }

check for undefined or null

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.

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)

php style function check in javascript

I don't recollect exactly what its called but php checks and exists as soon as it finds one false value in a function, like so :
// php
if(isset(a["hi"]) && isset(a["hi"]["hello"])) {
}
here if a["hi"] is not set, it will not bother to check the next a["hi"]["hello"].
to check something like this in javascript you'd need to nest
// js
if(typeof(a["hi"]) != "undefined") {
if(typeof(a["hi"]["hello"]) != "undefined") {
}
}
Can we do php style lazy checking in javascript?
(I may not be using the best method to check if an element exists in a multi-dimentional array, if there is a succinct way of doing this, would love to know.)
thanks!
You could use in to check property existence.
if(a && ('hi' in a) && ('hello' in a['hi'])) {
}
if(a.hi === undefined && a.hi.hello === undefined) {
if you know that a.hi can never be a falsey value (null / false / 0) you can do
if(!a.hi && !a.hi.hello) {
Checking the typeof a variable for the string "undefined" is equivalent to using === to check whether the variable is the same data type as the undefined keyword. You can therefore reduce the nested if statements to a single statement:
if(a.hi !== undefined && a.hi.hello !== undefined) {
// do something with a.hi.hello
}
It's worth noting that the statement above assumes that a is not null when the if statement takes place which could cause errors. It also holds true that if you require a.hi.hello to be present for the if statement to evaluate, then you can use falsy checking for a and a.hi as they would need to be object types for (which are non-falsy):
if(!!a && a.hi && a.hi.hello !== undefined) {
// do something with a.hi.hello
}

Categories

Resources