Strange Javascript behavior in simple situations. Explain


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
I, of course, after C++ it is difficult to find similar errors in your code. But still: what explains this behavior of JS?

An example of the first. In both cases, expected true:
var flag = true;
flag &= true;
alert(flag);//says "1"

var flag = true;
flag = flag && true;
alert(flag);//says true


Will take a second. The comparison to "equality" of two objects-date always returns false. In this case <= < return different values. It is not clear.
alert(new Date == new Date);//says "false"
alert(new Date <= new Date);//says true
alert(new Date >= new Date);//says true
alert(new Date < new Date);//says "false"
alert(new Date > new Date);//says "false"
by | 4 views

2 Answers

0 like 0 dislike
Maybe because
flag &= true;
it
flag = flag & true;
not
\r
flag = flag && true;
(count the number of ampersands).
\r
With respect to dates. Here such code works:
\r
var a = new Date; var b = a; alert(a == b); // true 
by
0 like 0 dislike
>flag &= true;
\r
here type conversion Boolean -> Number
\r
>alert(new Date == new Date);//says "false"
\r
each element is a new object, it is necessary to look in what order they are created.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
28,013 users