getAttribute('value') when value="" - javascript

I am using Y.one(selector).getAttribute('value'); to return the value on a page and I am having trouble dealing with cases where value="".
var check_value = Y.one(selector).getAttribute('value');
if (check_value != undefined || check_value != null || check_value != "") {
alert(check_value);
}
With all of these checks I am getting an empty alert box when the value of the element I am looking at is "". In this case since I know the value I am looking for is a number I can change the check to just look for a number > 0 and have it work but I was wondering if anyone knew of a method to check for no value for cases where I was not dealing with numeric data.
if (check_value >0) {
alert(check_value);
}
This method does work in my case.

Why don't you just check for 'truthiness' instead?
if (check_value) {
alert(check_value);
}
It's basically the same as your (intended) check, I suppose. And if you need to check for 0 (a Number), just use this:
if (check_value || check_value === 0) {
...
}

If it is an empty string, then it isn't undefined and it isn't null. Since you check if it is not undefined or not null or not an empty string, then it will pass.
Use && not ||

You might look at Y.Lang.isValue()
http://yuilibrary.com/yui/docs/api/classes/Lang.html#method_isValue

Look at the logic in plain english check_value != undefined || check_value != null || check_value != "" if it's undefined or null or "", if it's null it's not undefined, if it's blank it's not null, so everything gets through; you're looking for a logical AND instead of OR
try this:
var check_value = Y.one(selector).getAttribute('value');
if (check_value != undefined && check_value != null && check_value != "") {
alert(check_value);
}

You're seeing the alert because your if statement has OR (||) clauses. In the case that the value attribute is an empty string, the last condition (check_value != "") should fail. However, since "" is not equal to undefined the first condition has already returned true, the if statements test passes (it won't evaluate the other conditions because it doesn't need to) and the if block is executed.
I suspect you actually wanted to use AND (&&) instead.

Related

JS Remove Entries from 2D array

Totally newbie to JS so apologise for the probably obvious solution to this issue!
I'm trying to write a bit of code for use in google sheets that basically removes elements of a 2D array if the 3rd value in each nested array is empty eg.
[[1,2,3],[4,5,,],[7,8,9],[,,,]
would become
[[1,2,3],[7,8,9]]
I've currently got:
if (bkLoc=='HALL'){
var sumRng = sh.getRange('MIC_SUMM').getValues();
for (var i =0; i<sumRng.length ; i++){
if(sumRng[i][2] !== (undefined || '' || null)){
micSumm.push(sumRng[i]);
}
}
}
But the output seems to contains loads of empty arrays almost like its pushing every loop and I'm not sure why.
Any help would be gratefully received!
EDIT1: So with the help of you guys I got it to work. Using Nikhils answer I am now using this for the IF
if (bkLoc=='HALL'){
var sumRng = sh.getRange('MIC_SUMM').getValues();
for (j =0; j<sumRng.length ; j++){
var x = sumRng[j][2];
if(x != 0 && x != '??' && x != undefined){
micSumm.push(sumRng[j]);
}
}
}
But to be honest I don't really understand it. My understanding was || is OR so in my original code
if(sumRng[i][2] !== (undefined || '' || null))
If the tested content DOESN'T CONTAIN undefined OR "" OR null , the if statement should be true. I thought && meant AND so I'm unclear as to why that ever passes
Apologies for being so dumb!
Cheers
In your current if condition, undefined || '' || null evaluates to null. Hence, the condition eventually becomes sumRng[i][2] !== null.
However, as you need to check for undefined and '' too, you will need to update your condition
From
if(sumRng[i][2] !== (undefined || '' || null)){
to
if(sumRng[i][2] !== undefined && sumRng[i][2] !== '' && sumRng[i][2] !== null){
Assuming you've defined micSumm somewhere and started out with a blank array in it (var micSumm = [];), then you're on the right track, the issue is here:
if(sumRng[i][2] !== (undefined || '' || null)){
That's not how you do a check against multiple values in JavaScript. The way that's evaluated is:
sumRng[i][2] is evaluated; let's call the resulting value left
undefined || '' is evaluated, the result is ''; let's call it temp¹
temp || null ('' || null) is evaluated, the result is null; let's call that right
The result of left !== right is evaluated
So you end up only checking for null, not undefined or ''.
Instead:
var value = sumRng[i][2];
if (value !== undefined && value !== '' && value !== null) {
Or you can take advantage of the fact != undefined also checks for null:
var value = sumRng[i][2];
if (value != undefined && value !== '') {
...but that can be a bit less clear.
¹ Why does undefined || '' result in ''? Because an expression a || b is evaluated like this:
Evaluate a
If the value from Step 1 is truthy, make that the result of the || operation
Otherwise, evaluate b and make that the result of the || operation
undefined is falsy, not truthy. (A falsy value is any value that coerces to false when used as a boolean. The falsy values are undefined, null, "", 0, NaN, and of course, false. All other values are truthy.)
You could also use filter(which is supported by apps-script):
var filteredArr = sumRng.filter(function(e) {return e[2];})
Note that you're getting a 2D array with a specific rectangular dimension. So, there's no possibility of a value being undefined. Unless you specifically made a value null,null isn't returned. As noted in the comments below, the above also filters out 0 and boolean false. So, You can use
sumRng.filter(function(e) {return e[2].toString();})
or
sumRng.filter(function(e) {return e[2] || e[2] === false || e[2] === 0;})
References:
Array#filter
Primitives
null
Comparison Operators
Truthy
I still struggle with the why the original OR method I had doesn't work. In fact I went over to reddit to try and see if I could get anymore clarification but still it won't go in. But I appreciate all your help trying.
I did get an alternative solution to my conundrum though which seems a more condensed version of what I was using. That was simply this (courtesy of user insertAlias)
if(sumRng[i][2]){}
Apparently that will only pass for anything truthy so seems to fit the bill. But please point out any shortcomings

Difference between exclamation equals sign and exlamation 2x equals sign when checking with null

What is the difference between next if statements in javascript when checking with null?
var a = "";
if( a != null ) // one equality sign
....
if( a !== null ) // two equality sign
....
When comparing to null I can't find any difference.
According to http://www.w3schools.com/js/js_comparisons.asp
!= - not equal
!== - not equal value or not equal type
In JavaScript, null has type: object (try yourself executing the following sentence typeof null).
That is, !== will check that a is also object before checking if the reference equals.
Actually you know that === and !== are meant to check that both left and right side of the equality have the same type without implicit conversions involved. For example:
"0" == 0 // true
"0" === 0 // false
Same reasoning works on null checking.
!= checks
negative equality
while !== checks for
negative identity
For example,
var a = "";
a != false; //returns false since "" is equal false
a !== false; //returns true since "" is not same as false
but if you are comparing it with null, then value will be true in both ocassion since "" is neither equal nor identical to null
There is no difference between them when comparing to null.
When we use strict equality (!==) it is obvious because they have different types, but when we use loose equality (!=) it is an important thing to remember about JavaScript language design.
Because of language design there are also some common questions:
How do you check for an empty string in JavaScript?
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
var a = "";
(1) if( a != null ) // one equality sign
Above condition(1) checks value only and not data-type, this will return true.
....
(2) if( a !== null ) // two equality sign
This checks value and data-type both, this will true as well.
To understand it more precisely,
var a = "1";
if( a == 1) {
alert("works and doesn't check data type string");
}
if( a === 1) {
alert('doesn't works because "1" is string');
}
if( a === "1") {
alert('works because "1" is string');
}
There is a difference if variable has value undefined:
var a = undefined;
if( a != null ) // doesn't pass
console.log("ok");
if( a !== null ) // passes
console.log("ok");
Got idea from reading this great post Why ==null, Not ===null. Also != is faster.

Eloquent Javascript Ch. 7 - assignment statement with boolean expression

I'm having difficulty understanding the "atDest" variable declaration in this lesson that appears to be taking on two values (both a boolean and an object).
http://eloquentjavascript.net/07_elife.html
actionTypes.eat = function(critter, vector, action) {
var dest = this.checkDestination(action, vector);
var atDest = dest != null && this.grid.get(dest);
if (!atDest || atDest.energy == null)
return false;
critter.energy += atDest.energy;
this.grid.set(dest, null);
return true;
};
Any tips here would help. When I try testing out variables with similar syntax with console.log I've been noticing that the object value overrides the bool. Is this an inherent Javascript trait where a variable can have more than one value?
The boolean operators || and && actually return the value of one of the specified operands.
var a = 5 || false; // 5 is assigned to a
var b = true && 5; // 5 is assigned to b
The logical && returns the first expression if it can be converted to false, otherwise it returns the second expression (or 5 in the example above)
So if dest != null is not false in the following statement
var atDest = dest != null && this.grid.get(dest);
then atDest is assigned the value of this.grid.get(dest).
Here is the documentation you are looking for.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
http://dorey.github.io/JavaScript-Equality-Table/
The gist is that in JavaScript everything can be interpreted as boolean. The above doc makes it very clear. Values like null, undefined, NaN, 0, "" will be interpreted as falsy. What !atDest really means is basically to check if atDest is not assigned (likely null or undefined).
The next part is also worthy mentioning. atDest.energy == null is a loose equality check (notice the == instead of ===). This check will actually check for both null and undefined. But the difference between atDest.energy == null and !atDest.energy is that atDest.energy == null will not match 0, "", nor NaN.
JavaScript values can be falsy like
false
0 (zero)
"" (empty string)
null
undefined
NaN (a special Number value meaning Not-a-Number!)
or truthy, which is everything else including the empty object {}. Falsy values are equivalent to false in a boolean check and truthy values are equivalent to true in a boolean check.
Now, if you attempt to read a non-existent property on an object you'll get undefined. However if you attempt to read a non defined property off the special value of null or undefined, you will get an error. Something like
Unable to get property 'energy' of undefined or null reference
if you attempt to read atDest.energy when atDest is null or undefined.
So if you want to check if a property is of value xyz on an object that you are not sure is null or undefined, you have to do 2 things
First check if you can actually read the property without error i.e. that the object is not null or undefined.
Then read the property and do the check.
In your example, this
if (!atDest || atDest.energy == null)
does pretty something equivalent. This part
!atDest
evaluates to false if atDest is null or undefined (because both these values are falsy) and short circuits the loop (i.e. doesn't read atDest.energy). In effect what the code attempts to do is this
if (atDest === undefined || atDest === null) {
.... do something ....
else if (atDest.energy == null) {
.... do same something ....
else
.... do something different ....

How to skip the second part of (JavaScript if statement) if the first part is false

I have this code:
if (window.content.document.getElementById("error-msg") != null )
{
if (window.content.document.getElementById("error-msg").offsetParent !== null)
{
...
}
}
Can it be written in one if statement?
I tried the following...
if ( (window.content.document.getElementById("error-msg") != null) || (window.content.document.getElementById("error-msg").offsetParent !== null) ) {}
But, it didn't work, and produces an error:
TypeError: window.content.document.getElementById(...) is null
The common idiom is to use the && operator like this
var errorMsg = window.content.document.getElementById("error-msg");
if (errorMsg && errorMsg.offsetParent) {
...
}
Here, JavaScript will evaluate errorMsg first and if it is Truthy, then it will evaluate the errorMsg.offsetParent part. The condition will be satisfied only if both the expressions in && are Truthy.
Note: The Truthy evaluation will return false, if the expression being tested is 0, false etc (See the list of Falsy values here). So, if you want to test if they are not null, just write that explicitly, like this
if (errorMsg !== null && errorMsg.offsetParent !== null) {
...
}
On the other hand, the || operator will evaluate the second operator only if the first expression is Falsy. In your case, if
(window.content.document.getElementById("error-msg") != null)
is true, it means that getElementById("error-msg") returns null. Since the first expression is evaluated to be Truthy, it evaluates the other expression and it effectively tries to check
null.offsetParent !== null
That is why it fails.
Maybe you want to use &&
if (a != null && b != null) {
// Do something.
}

Logical expression

Is there any difference between the below statements:
if(newValue && newValue != '') and
if(newValue != '')
I have observed expression 1 in many scripts but always got confused.
Please assist!
if(newValue && newValue != '').
This guards against a value of null or undefined.
Out of the possible '', 0, false, undefined and null, only the last two are not equal to '' (using !=), requiring the extra condition.
console.log(null && null != '') // null -> falsy
console.log(null != '') // truthy
var undef = void 0;
console.log(undef && undef != '') // undefined -> falsy
console.log(undef != '') // truthy
Answer is no,
1) for (newValue && newValue != ''),it checks whether newValue exist(non false values) and its not empty
2) for if(newValue != ''),it only checks whether newValue is not empty
Just one more thin I thought worth adding to the answers already posted.
As you, the OP, say: you've seen the first expression in existing code many times, so you've probably seen things like this:
if (foo && foo.bar)
{
foo.bar();
}
In this case, this is to avoid errors in certain browsers. In most modern browsers accessing somehting like localStorage.doesNotExist won't raise errors, but not all browsers support this so we: first check if the localStorage object exists, and if so, if the property can be resolved to a non-falsy value.
The same logic applies to methods of objects, that may be different in various browsers:
var domElement = document.getElementById('foobar');//might not exist, though
if (domElement && domElement.addEventListener)
{//does element exist && does it have a method called addEventListener
domElement.addEventListener('click', functionRef, false);
}
else if (domElement && domElement.attachEvent)
{
domElement.attachEvent('onclick', functionRef);
}
if you were to omit the first domElement in the if statement, and simply write domElement.addEventListener, that could be the same as writing null.addEventListener which throws a TypeError (because you're trying to access a property on a primitive value)

Categories