Avatar ·

Can (a == 1 && a == 2 && a == 3) be true in JavaScript?

Hi everyone! I'm new to JavaScript and came across a strange interview question. I was asked: can the expression `(a == 1 && a == 2 && a == 3)` ever be true? At first glance, this seems completely absurd. How can the same variable `a` simultaneously equal 1, 2, and 3? In any normal programming language, this is impossible. But I was told that JavaScript has tricks that allow this to happen. I understand that this is related to the peculiarities of type coercion in JavaScript, but I can't figure out exactly how it works. If we use strict comparison `===`, then such a trick definitely won't work, but with loose `==` there are some loopholes. Maybe someone can explain how this is possible? I'm interested not only in specific implementations