wrong if statement evaluation, document.cookie - javascript

I`m trying to perform some action with document.cookie,
I checking some If conditional mentioned below:
if((!document.cookie.indexOf('cookies:all_checked') >= 0) ||
(!document.cookie.indexOf('cookies:first_and_second') != -1) ||
(!document.cookie.indexOf('cookies:first_and_third') != -1) ||
(!document.cookie.indexOf('cookies:first_only') != -1)){
createCookies();
}
before this statement I create cookie like this
var variable = document.cookie == 'cookies:all_checked'
the trouble is no matter I`m trying do check
(!document.cookie.indexOf('cookies:all_checked') >= 0)
or the same without ! mark I get true with this condition. How to check is it cookies with this content is really setted?

Your problem is that
document.cookie.indexOf('cookies:all_checked')
will return either the index, or -1 (when not found).
In your example, it will return 0 because document.cookie starts with the string you're looking for.
Then, !0 will evaluate to true as 0 evaluates to false, and true evaluates to 1, which is indeed >= 0.
And 0 is also >= 0.
That's why you get true everytime.
You should try:
!(document.cookie.indexOf('...') >= 0) // false
document.cookie.indexOf('...') >= 0 // true
Note that !anyNumber will result in either true or false which will both be >= 0 as true will evaluate to 1 and false to 0 which are both >= 0
Therefore, !anyNumber >= 0 will always be true.
As a side note, please be aware that:
var variable = document.cookie == 'cookies:all_checked'
does not set/create your cookie.
What it does is check whether cookie equals 'cookies:all_checked' and assigns this result (true or false) to variable.

Related

Whats the difference between (a==1 || b==1) and ((a || b)==1)

Whats the difference between (a==1 || b==1) and ((a || b)==1)
this code block works right
if(userName==="" || userAge==="" ){
addErrorBool(true);
return;}
but this one not
if((userName || userAge)==="" ){
addErrorBool(true);
return;}
whats does the second one do?
a || b will evaluate to a, if a is truthy, otherwise it'll evaluate to b. So ((a || b)==1) will take whichever value that was and compare it against 1.
For example
(0 || 5) == 1
// equivalent to
(5) == 1
(1 || 2) == 1
// equivalent to
(1) == 1
For what you want, use .some instead, if you want to keep things DRY.
if ([userName, userAge].some(val => val === '')) {
addErrorBool(true);
return;
}
And then you can add as many items to the array .some is called on that you want.
(userName || userAge)==="" means:
(userName || userAge): if userName is truthy, use this value. Otherwise use userAge
==="": compare whichever object was chosen above, and compare that this is a string with no contents.
userName==="" || userAge==="" means:
userName==="": compare userName to see if it is a string with no contents
if it is, the result is true, otherwise:
userAge==="": compare userAge to see if it is a string with no contents
if it is, the result is true, otherwise the result is false

In Javascript, when using an if statement to compare zero and not false with triple equal signs, why is not false equal to false?

I'm creating a typing test in Javascript. When the user presses the keyboard button, the typing test begins if zero keys have been pressed previously. If textEnteredLength (keys pressed) is === 0 and !timerRunning is equal to not false. Why does this function run? 0 === not false, is not a true statement, right?
var timerRunning = false;
function start() {
let textEnterdLength = testArea.value.length;
if (textEnterdLength === 0 && !timerRunning){
timerRunning = true;
interval = setInterval(runTimer, 10);
}
Due to operator precedence, this expression:
textEnterdLength === 0 && !timerRunning
is equivalent to
(textEnterdLength === 0) && !timerRunning
The 0 isn't compared against !timerRunning, but against textEnterdLength.
So, in the end it's
if (someBoolean && someOtherBoolean)
and if both are true, it'll run.

JavaScript: When the `condition` portion of a `ternary` or `if` statement does not include `===` or `>=`

In the condition portion of the following ternary statement, does playlist.length equal playlist.length >= 1 ?
var playlist = ["video1", "video2", "video3", "video4", "video5"];
// some code here
alert ((playlist.length) ?
playlist.length + "video(s) remain in the playlist: " + playlist.join (", ") + "."
: "No videos remain in the playlist");
Likewise, in the following snippet of code, does ! playlist.length equal playlist.length === 0 ?
alert ((! playlist.length) ?
"No videos in the playlist."
: playlist.length + " video(s) remain in the playlist: " + playlist.join(", ") + ".");
This is the first time I've seen an example where the condition portion of a ternary or if statement does not include such a notation as === or >=. So I'm not sure how to interpret this.
0 is implicitly converted to false in boolean comparisons in JavaScript. So when the length is 0, that is false. Conversely, any other number is implicitly converted to true in boolean comparisons in JavaScript. So when the length is anything but 0 it is true.
An easy test is to use !! to see the "truthy" value
!!1 === true
!!0 === false
!!6 === true
The part to the left of the ? is simply evaluated for "truthiness". The value 0 is "falsy" so evaluates as false for the purposes of the test. Numeric values other than 0 are "truish" and therefore for this purpose evaluate to true.
All other behaviors of the ternary are the same.
The === and !== simply add the additional constraint that the L-value and R-value must also be the same type.
The two are very similar: !playlist.length and playlist.length === 0.
However, they are not exacty the same. In fact, here:
var playlist1 = [];
var playlist2 = {};
!playlist1.length // true
!playlist2.length // true
playlist1.length === 0 // true
playlist1.length === 0 // false
In that sense !playlist.length also can be used on all kinds of objects, not just arrays.
In any case, when using this on an array, it is a way to check if the array is empty, and works as you have suggested, the same as playlist.length === 0.
In javascript 0 equals false, and any other number value equals true, but if you use === it compare value types too.

function not executing on comparison of two arrays

I am trying to get a comparison operator to work, without success. The operator compares two arrays to ensure they are identical.
if (($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0)) {
alert("all matches dropped");
}
The code works of course with 'true' in place of the comparison.
if (true) {
alert("all matches dropped");
}
The strange part is that the comparison returns 'true' when entered into the console:
console.log($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0)
----> true
Any ideas what may be wrong? Thanks.
It should be:
if($(array_1).not(array_2).length === 0 && $(array_2).not(array_1).length === 0)
Instead of:
if (($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0))
Here $(array_1).not(array_2).length and ($(array_1).not($(array_2)).length both are not the same thing.

how do i make this javascript logic + syntax (very simple)

Why I am getting true all the time?
var _template_id = "";
if (_template_id != '0' || _template_id!="") {
alert("true")
}else{
alert("false")
}
or even if i set _template_id="0", it still turns out as true...
Because you're asking if _template_id isn't equal to "0" OR isn't equal to "". One of those will always be true.
Proof: Given your statement of (_template_id != '0' || _template_id!=""), let's suppose that the first part is false. Therefore _template_id == '0' is true, and hence _template_id != "" is true, so overall the statement evaluates to true.
If, on the other hand, the first part is true, then clearly the whole thing evaluates to true again.
Therefore, the statement is always true.
You want && not ||.
It must always be true because "0" != "", so one or the other is always true.
a != b || a != c is always true when b and c are different.
What you want here is probably to use
if (!(_template_id == '0' || _template_id =="")) {
alert("true")
Then if you set _template_id="0" you would get false as desired.
You probably want
!(_template_id == '0' || _template_id == "")
If _template_id is neither 0 nor ''.
This is equivalent (thanks to De Morgan's laws) to:
_template_id != '0' && _template_id != ""
Where are you trying to change the value of _template_id ? I will always be true if you assign "" and then ask if its '0' or "" then true.
You are logically going wrong.
Either use == and ¦¦
Or
Use != and &&
It will result the same in both cases and your condition will be true as well.

Categories