문제

I have been wondering that are these two ways of performing variable setting always giving me the same results:

var t = test ? test : 0

and

var t = test || 0

Because the latter is a bit faster way to do it than the first one and for now I have seen it working exactly the same but was just wondering is this just an illusion and is it possible that it will behave unexpectedly in some specific situations?

도움이 되었습니까?

해결책

Yes, those examples are completely equivalent.

다른 팁

The first is called the ternary operator and can also be used to evaluate an expression. See

Question Mark in JavaScript

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top