var strExt = GetAttributeFromItemTable(itemTable, "Ext", "FileType");
I did a alert for strExt and it resolves.
if (strExt!='wav')
{
// this works
}
if (strExt!='wav' || strExt!='mp3')
{
// this does not work.
}
Your logic is flawed:
if your variable strExt was equal to 'wav' it would not be equal to 'mp3', and versa-visa.
Please explain your desired results more clearly.
I think what you're trying to say is something like (neither 'wav' nor 'mp3'):
if ( !( strExt == 'wav' || strExt == 'mp3' ) )
which is logically equivalent to (not 'wav' and not 'mp3'):
if ( strExt != 'wav' && strExt != 'mp3' ) )
|| says: if any condition is true, It'll return true, without looking at the ones after it.
So true || false is true, false || true is true.
In your case, you say "if strExt is not equal to wav and is not equal to mp3, then execute the code". In case that one of them is true, it executes.
I'm thinking that you're looking for the && symbol. The logical and says "I'll return true only if every condition is true" - when it hits a false, it returns false.
What I think your code should look like:
if (strExt!='wav' && strExt!='mp3')
I would expect the result of the second expression to be True regardless of the value of the string strExt (any extension is either not equal to "wav" or not equal to "mp3"). What do you expect?
Are you certain you don't mean a Boolean AND instead of an OR ("extension is neither 'wav' nor 'mp3'")?
just add the another parenthesis, like so:
if ((strExt!='wav') || (strExt!='mp3'))
{
// this does not work.
}
The logic behind this makes no sense wouldn't you want to do:
if ((strExt !== "wav") && strExt !== "mp3") {
}
AND instead of OR makes more sense in this situation.
If you want to get into second if, when strExt is not equal to both 'wav' and 'mp3' you need to use an && operator.
if (strExt!='wav' || strExt!='mp3')
when strExt='wav' => strExt!='wav' = false; strExt!='mp3' = true => false or true = true and gets into if statement and is the case is similar when strExt='mp3' .
Related
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.
want to check null values. any() method returns null or array of matched result (actually there's a match() method inside which is returned).
$scope.isMobileBrowser = !isMobile.any() ? false : true;
If any() method returns null I want false to be assigned to $scope.isMobileBrowser variable, otherwise true. will the over mentioned snippet fail in any probable case? Is there any other more efficient workaround?
for more details of isMobile object:
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
Empty string is also a falsy value.
If any() returns an empty string, !isMobile.any() ? false : true will return false, but you probably want true.
This means your code is incorrect for this case.
I'd just do something like isMobile.any() !== null.
As per the any() function, you are returning value of the following expression:
(isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS()
|| isMobile.Opera() || isMobile.Windows())
Each of these functions can either return an Array or null as seen in the doc for match
So while evaluating the OR it will evaluate to the first truth value encountered and doesnt evaluate any further as the expression is already fit to be true. So, for example if the browser is android the expression evaluates to ["Android"]. If windows it will be ["Windows"]. If none of these, it will be null. Which makes it clear that any() can only return an Array or null.
isMobileBrowser should be true if it's any of these mobile browsers, which means isMobileBrowser should be true if:
any() evaluates to an Array
OR in other way:
If any() does not evaluate to null
which is:
$scope.isMobileBrowser = isMobile.any() instanceof Array;//looks messy
$scope.isMobileBrowser = (isMobile.any()).constructor === Array;//looks messy
$scope.isMobileBrowser = Array.isArray(isMobile.any());//looks messy
$scope.isMobileBrowser = Object.prototype.toString.call(isMobile.any())
=== "[object Array]";//looks messy
OR the other way:
$scope.isMobileBrowser = isMobile.any() !== null;
$scope.isMobileBrowser = !(isMobile.any() === null);
isMobileBrowser = !(Object.prototype.toString.call(isMobile.any())
=== "[object Null]");//looks messy
So we just discussed different ways to check for null and Array. You have two possible sets of outputs
null value which is always false
An Array which is always true (You can check this empty array scenario although that doesn't apply here)
So you can simply do the following to convert those to exact boolean without worrying much:
isMobileBrowser = Boolean(isMobile.any()); //to convert value to boolean
isMobileBrowser = !!isMobile.any(); //another way to convert to boolean
//!!["Android"] is true
//!!null is false
#rossipedia explains the !! well in his answer.
A compact way of representing what you want would be:
$scope.isMobileBrowser = !!isMobile.any();
The !! there does two things:
The first ! evaluates the "truthiness"1 of the return value of isMobile.any() and then negates it.
The second ! negates that value again.
So what you end up with is false if .any() returns null, otherwise true.
However, this may fail in edge cases where .any() returns something that is "falsy". In that case, checking for null specifically is what you want:
isMobile.any() !== null
1: "Truthiness":
In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).
From MDN
Try this:
$scope.isMobileBrowser = isMobile.any() === null;
The code below represents the idea I am trying to achieve but when I test it doesn't work, what would be the appropriate way to test if q1 and q2 is equal to true?
function processForm() {
if(q1_valid = true && q2_valid = true){
alert("yes");
} else {
alert("no");
}
}
When you use simple = in javascript (and most C-like languages), what happens is that you assign the variable, then return the result of said assignment.
For instance, take the code a = b = true. This can be split up into a = (b = true). Now, if we only look at the part inside the parenthesis, you'll see that what it does is first set b to true, then return b. Then, outside the parenthesis it sets a to whatever b was (which ofcause is true), and returns the value of a. The value of a has nowhere to go, so it's simply dropped.
Now, if we go back to your if-test, what you end up with is basically this:
Set q1_valid to true.
return true (the value of q1_valid) to the && operator.
true is valid for && so it looks at right hand side.
Set q2_valid to true.
return true to the &&.
&& now has true on both sides. Returns true.
Test always passes. q1_valid and q2_valid will always be true after test is run.
The simple solution is to replace = with either == (equals) or === (type and value equals). Your if-check should look like one of the following:
1.
if(q1_valid == true && q2_valid == true)
2.
if(q1_valid === true && q2_valid === true)
Also, since working with booleans (values that are either true or false), the check for equality to true can be omitted altogheter. Another way to do this is simply like this:
if(q1_valid && q2_valid)
Two issues here:
You need to use two equals signs for comparison ==
The variables don't exist in the function, you would need to pass them as parameters when calling the function
function processForm(q1_valid, q2_valid) {
if(q1_valid == true && q2_valid == true){
alert("yes");
} else {
alert("no");
}
}
Is there a special reason to do:
if (!options || (options && options.booleanCondition))
Instead of:
if (!options || options.booleanCondition)
I'm no javascript guru, so perhaps there's a special case or reason why the author of that code wrote it that way.
i don't see a reason to do
if (!options || (options && options.booleanCondition))
I'm no Guru but i'd stick to
if (!options || options.booleanCondition)
and save a check to the fact that options is true: in fact if options is not true the first condition is true and the second is never evaluated since it's an or condition.
That's what i think
Perhaps it's using a getter. This is a rediculous example but it can be the reason:
var o = {};
var i = 0;
o.__defineGetter__('options', function() {
return (i++) % 2 === 0 ? {booleanCondition: true} : null;
});
o.options; // Object first time
o.options; // null second time
This means:
if (!o.options || (o.options && o.options.booleanCondition))
!o.options is false (negating an object), but after that o.options is null (falsy) so then the check is mandatory.
if options.booleanCondition can change, then yes. If options is false, then the If condition will be true. If options is true, and boolean.Condition is false, then the If condition will be false. If option is true, and boolean.Condition is true, then the If condition will be true.
UPDATE
Actually, I guess it wouldn't be needed - since your simplification would be (if !true or true) if options is true, so the result would be same as (if true or true).
var DEST_VALUE = 1
var APPDAYS_AFTER = 2
}
How can i check whether the variable holds some value or not. When i do this, it does not work...
Of course it doesn't do anything, because in your example DEST_VALUE resolves to true like APPDAYS_AFTER. Value that resolve to false when converted to a boolean in javascript are:
false
null
undefined
The empty string ''
The number 0
The number NaN (yep, 'Not a Number' is a number, it is a special number)
if you write
if(!DEST_VALUE){
txtSiteId.value = fileContents.Settings.SiteID;
}
you write "if DEST_VALUE is not true do something" (in your case it does nothing). If you want to check if a variables hold a value:
if(DEST_VALUE !== undefined){
//do something
}
I use such function to check if variable is empty or not:
function empty( mixed_var ) {
return ( typeof(mixed_var) === 'undefined' || mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false );
}
I assume you mean "holds some value" like in "the variable has been created so it exists", right? Otherwise, your approach works perfectly fine.
If you want to check whether a variable exists in javascript, you have to check its parent object for the property - otherwise the script will fail. Each object in javascript belongs to a parent object, even if it seems to be global (then, it belongs to the window object). So, try something like:
if (window.DEST_VALUE)
// do something