comparison-operators

Why [] == [] is false in JavaScript?

梦想的初衷 提交于 2019-11-27 06:02:28
问题 I am working on a part of the code where I have an array which looks like [[data]] . The data is rendered on the server side through the Django template engine. So my code looks like this: var data = {{ series|safe }}; // data will be [[]] if no data is present if (data ==[[]]) console.log('no data'); The if always returns false . That means in [[]] == [[]] is false and my test shows that []==[] is false as well. Any descriptions would be appreciated. 回答1: Because == (and === ) test to see if

How do chained comparisons in Python actually work?

牧云@^-^@ 提交于 2019-11-27 06:00:57
问题 The Python Doc for Comparisons says: Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z , except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). And these SO questions/answers shed some more light on such usage: Python comparison operators chaining/grouping left to right? What does "evaluated only once" mean for chained comparisons in Python?, in particular the currently-accepted answer So

Find substring in the string in TWIG

感情迁移 提交于 2019-11-27 05:44:04
问题 I want to find substring of the string or check if there is no such substring using Twig. On the words, I need analogue of 'strstr' or 'strpos' in php. I googled and searched this issue in stackoverflow but nothing found. Does someone know how to solve this problem? 回答1: Just searched for the docs, and found this: Containment Operator: The in operator performs containment test. It returns true if the left operand is contained in the right: {# returns true #} {{ 1 in [1, 2, 3] }} {{ 'cd' in

Implicit data type conversion in JavaScript when comparing integer with string using ==

不想你离开。 提交于 2019-11-27 04:40:45
The code: var num = 20; if(num == "20") { alert("It works"); } else { alert("Not working"); } The question: In C programming we have a rule name data type promotion, where when there's a mix of data type (example: addition of integer and floating point), the integer will first converted to floating point before the addition is being carry out. The code above will prompt me an alert box with the message "It works" that shows the if test condition is evaluate to true. For loosely typed JavaScript, I'm just curious: is there any rule like C that determines which conversion will be carry out in

C# Nullable Equality Operations, Why does null <= null resolve as false? [duplicate]

让人想犯罪 __ 提交于 2019-11-26 22:49:38
问题 This question already has answers here : Why does >= return false when == returns true for null values? (8 answers) Closed 2 years ago . Why is it that in .NET null >= null resolves as false, but null == null resolves as true? In other words, why isn't null >= null equivalent to null > null || null == null ? Does anyone have the official answer? 回答1: This behaviour is defined in the C# specification (ECMA-334) in section 14.2.7 (I have highlighted the relevant part): For the relational

C# Type Comparison: Type.Equals vs operator ==

人走茶凉 提交于 2019-11-26 20:31:00
问题 Resharper suggests that the following be changed from: Type foo = typeof( Foo ); Type bar = typeof( Bar ); if( foo.Equals( bar ) ) { ... } To: if( foo == bar ) { ... } operator == // Summary: // Indicates whether two System.Type objects are equal. // // Parameters: // left: // The first object to compare. // // right: // The second object to compare. // // Returns: // true if left is equal to right; otherwise, false. public static bool operator ==( Type left, Type right ); Equals( Type o ) //

What does “===” mean?

浪子不回头ぞ 提交于 2019-11-26 20:27:34
I've noticed someone using the PHP operator === which I can't make sense out of. I've tried it with a function, and it corresponds in crazy ways. What is the definition of this operator? I can't even find it in the declaration of PHP operators. Tim Sylvester $a === $b (Identical) TRUE if $a is equal to $b , and they are of the same type. (introduced in PHP 4) PHP Docs Dykam http://www.php.net/ternary $a == $b Equal TRUE if $a is equal to $b, except for (True == -1) which is still True. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. > "5" == 5; True > "5" === 5;

Math-like chaining of the comparison operator - as in, “if ( (5<j<=1) )” [duplicate]

醉酒当歌 提交于 2019-11-26 18:02:03
This question already has an answer here: Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so? 5 answers int j=42; if( (5<j<=1) ) { printf("yes"); } else { printf("no"); } Output: yes Why does it output yes? Isn't the condition only half true? ShinTakezou C does not understand math-like syntax, so if(1<j<=5) is not interpreted as you expect and want; it should be if (1 < j && j <= 5) or similar. As explained in other answers, the expression is evaluated as ((1 < j) <= 5) => ("true" <= 5) => "true" where "true" (boolean value) is implicitly converted to 1, as explaneid e.g.

JavaScript comparison operators: Identity vs. Equality

*爱你&永不变心* 提交于 2019-11-26 16:34:45
I've been trying to understand the difference between JavaScript's comparison operators: identity and equality. From what I've read, if you check the equality of two objects using ==, JavaScript will try to figure out if they are the same type and, if not, try to get them to that same type. However, === doesn't behave in the same manner. So as an example: var n = "1"; console.log(n==1); // outputs true console.log(n===1); // outputs false So what is the difference between these "identity" operators and the regular equality operators? What is the benefit of having both? Are there differences in

Why is “!=” used with iterators instead of “<”?

不羁岁月 提交于 2019-11-26 12:26:06
问题 I\'m used to writing loops like this: for (std::size_t index = 0; index < foo.size(); index++) { // Do stuff with foo[index]. } But when I see iterator loops in others\' code, they look like this: for (Foo::Iterator iterator = foo.begin(); iterator != foo.end(); iterator++) { // Do stuff with *Iterator. } I find the iterator != foo.end() to be offputting. It can also be dangerous if iterator is incremented by more than one. It seems more \"correct\" to use iterator < foo.end() , but I never