F
F
flm2010-10-30 11:10:21
JavaScript
flm, 2010-10-30 11:10:21

Incomprehensible Javascript behavior in simple situations. Explain

Of course, after C ++ it is difficult for me to find such errors in the code. But still: what explains this behavior of JS?

Example one. In both cases, true was expected : Will accept the second one. An equality comparison of two date objects always returns false. However, <= and < return different values. Unclear.
var flag = true;
flag &= true;
alert(flag);//говорит "1"

var flag = true;
flag = flag && true;
alert(flag);//говорит "true"


alert(new Date == new Date);//говорит "false"
alert(new Date <= new Date);//говорит "true"
alert(new Date >= new Date);//говорит "true"
alert(new Date < new Date);//говорит "false"
alert(new Date > new Date);//говорит "false"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shsmad, 2010-10-30
@shsmad

Maybe because

flag &= true;
This
flag = flag & true;
not at all
flag = flag && true;
(count the number of ampersands).
About the dates. Here is the code that works:
var a = new Date;
var b = a;
alert(a == b); // true

W
Wott, 2010-10-30
@Wott

>flag &= true;
here the type conversion Boolean -> Number
>alert(new Date == new Date);//says "false"
each element is a new object, you need to look in what order they are created.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question