Do angular conditions work the same way as pure javascript? [duplicate] - javascript

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I wonder if there is the same difference between identity === and equality == operators in angular directives like in pure javascript?
For example is
ng-if="value === 'foo'
better than
ng-if="value == 'foo'
What I checked is that
ng-if="true == 1
passes but
ng-if="true === 1
doesn't, so it looks like it works the same way like pure js. On the other hand in angular source they use just equality check, even tho in js identity is preferred.
https://github.com/angular/angular.js/search?utf8=%E2%9C%93&q=ng-if
Which operator should we use in angular directives?
EDIT:
To clarify - I'm not asking about javascript conditions, this has been already answered on stack, my question is - is there any difference in conditions between pure js and angular directive conditions?

Main difference between "==" and "===" operator is that former compares variable by making type correction e.g. if you compare a number with a string with numeric literal, == allows that, but === doesn't allow that, because it not only checks the value but also type of two variable, if two variables are not of same type "===" return false, while "==" return true.
Since JavaScript support both strict equality and type-converting equality, it's important to know which operator is used for which operation. As I said that, === takes type of variable in consideration, while == make type correction based upon values of variables, following are couple of more differences between "==" and "===" operator in JavaScript programming language :
1) When we compare two variables of different type e.g. a boolean with a string or a number with String using == operator, it automatically converts one type into another and return value based upon content equality, while === operator is strict equality operator in Java, and only return true if both variable of same type and also contains same value. This will be much clear with following example of == and === operator in JavaScript :
0==false // true, because false is equivalent of 0
0===false // false, because both operands are of different type
2=="2" // true, auto type coercion, string converted into number
2==="2" // false, since both operands are not of same type
2) "==" operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It's much similar Java's equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other. In fact, you should always use "===" operator for comparing variables or just for any comparison.

Related

Javascript operator ||0? [duplicate]

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 5 years ago.
I'm trying to translate a Javascript function to C# when I encountered something I've never seen before:
return (a / (b - (c * G * (d || 0)))) || 0;
C# is complaining about the d || 0 portion being applied to floating point number, and I have no idea what that Javascript operator does. (in C#, it's a logical OR)
Edit: all variables are floating point numbers.
The || operator returns the expression on the left side if that expression is truthy, otherwise it returns the expression on the right side. It's commonly used like this to specify default/fallback values, for instance, when a number is expected but the provided variable might contain undefined or null. It's also commonly used when optional arguments can be passed to a function and the function want's to simply use the first one that's truthy.
|| is the "short-circuited" OR operator and in order for it to work, both operands must first be evaluated as Booleans. From left to right, the value of the first expression that can be converted to true is returned (the original value, not the converted Boolean).
JavaScript will perform an implicit conversion to determine the "truthiness" of each operand if necessary, but C# will not. A float in C# is invalid with this operator because C# is a strongly-typed language and implicit conversions do not occur. You would have to explicitly cast your float to a Boolean to be able to use this operator in C#.
In C#, you'd need:
((bool) d || 0)
|| (OR)operator works when both the operands are Boolean. If not then JavaScript tries to implicitly convert it and perform the OR operation.
You need to explicitly cast in C#.

Javascript 0 == '0'. Explain this example

I found these examples in another thread but I don't get it.
0 == '0' // true
0 to the left I converted to false(the only number that does that). The right is a non empty string which converts to true.
So how can
false == true --> true
What have I missed?
Here is an official answer to your question (the quoted parts, link at the bottom) and analysis:
Equality (==)
The equality operator converts the operands if they are not of the same type, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
Syntax
x == y
Examples
3 == 3 // true
"3" == 3 // true
3 == '3' // true
This means, as I read it, that the first 3 (integer) is converted to string to satisfy the comparison, so it becomes '3' == '3', which is true, same as in your case with zeroes.
NOTE: I assume that the converion may vary in different JS engines, although they have to be unified under ECMAScript specifiction - http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 (quoted #Derek朕會功夫). This assumption is made on a subjective and imperative opinion that not all browsers and JavaScript engines out there are ECMAScript compliant.
Identity / strict equality (===)
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
The Identity / strict equality (===) found on the same resource at the end of the answer will skip the automatic type conversion (as written above) and will perform type checking as well, to ensure that we have exact match, i.e. the expression above will fail on something like:
typeof(int) == typeof(string)
This is common operator in most languages with weak typing:
http://en.wikipedia.org/wiki/Strong_and_weak_typing
I would say that one should be certain what a function/method will return, if such function/method is about to return numbers (integers/floating point numbers) it should stick to that to the very end, otherwise opposite practices may cut your head off by many reasons and one lays in this question as well.
The above is valid for other languages with weak typing, too, like PHP for example.
Read more of this, refer second head (Equality operators):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
When you use == JavaScript will try very hard to convert the two things you are trying to compare to the same type. In this case 0 is converted to '0' to do the comparison, which then results in true.
You can use ===, which will not do any type coercion and is best practice, to get the desired result.
Equality operator
The equality operator converts the operands if they are not of the same type, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
JavaScirpt Table Equality: http://dorey.github.io/JavaScript-Equality-Table/

jsHint error saying I should use "===" [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 8 years ago.
My code is giving me an error with jsHint. I am trying to do this:
if (data.result == 't' || task == 'show') {
But it tells me I should replace "==" with "===" can someone tell me why it gives this message?
=== is the strict equality operator.
== is the normal equality operator, == converts its operands to the same type if they are not already of the same type.
So there is a danger that something like "" == 0 will give you true although they are of different types.
Since there is implicit conversion involved which you might not no about because it is happening automatically, there is some danger and potential for errors or bugs that are hard to trace.
=== won't convert its operands, it will just compare them.
Silent type conversion can be a source of bugs. If you avoid converting between data types at comparison time, then you avoid many of those bugs.
=== means equality without type coercion. === operator will not do the conversion, it will only compare the operands.
The example given by sdfx here is very helpful in understanding this:
0==false // true
0===false // false, because they are of a different type
1=="1" // true, auto type coercion
1==="1" // false, because they are of a different type
From MDN:
JavaScript has both strict and Type–converting (abstract) comparisons.
A strict comparison (e.g., ===) is only true if the operands are the
same Type. The more commonly used abstract comparison (e.g., ==)
converts the operands to the same Type before making the comparison.
For relational abstract comparisons (e.g., <=), the operands are first
converted to primitives, then the same Type, before comparison.
Features of comparisons:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding
positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything,
including NaN. Positive and negative zeros are equal to one
another.
Two Boolean operands are strictly equal if both are true or both are false.
Two distinct objects are never equal for either strictly or abstract comparisons.
An expression comparing Objects is only true if the operands reference the same Object.
Null and Undefined Types are == (but not ===).

Is there a significant performance difference between '==' and '==='? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
I follow the practice of always using a absolute comparison '===', but some learned colleagues have argued that this is an unnecessary waste of computation time on type-checking in situations where you're highly confident of a particular comparison type.
So my question is this; is there realistically any significant performance hit for always type-checking values during comparison?
There's probably not a significant performance difference if the objects are already of the same type. Both operators have to look up and compare the types of the two arguments before doing the data comparison.
If the objects are of different types, === should be a little faster, since it can return false immediately, while == will coerce one of the arguments to the other type and then perform the comparison.
There's no significant performance difference between == and ===. The only difference between them is the type comparison. == comparison can be used even when the 2 conditions have different type. === comparison only accept comparison between 2 same type conditions. Example: when int and char can be compared with ==, === comparison can't be used.
There's a test of this at jsperf:
Take a look and try it yourself: http://jsperf.com/equalitydoubletriple/4
Edit: This test may be better, as #dystroy mentions in the comment below: http://jsperf.com/equalitydoubletriple/5

Difference between "==" and "===" in java script [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Hi
I am not clear why there are two == and === comparator operators in java script.
The equality operator == coerces, or converts the data type temporarily to see if it's equal to the other operand whereas the identity operator === doesn't need to do any converting whatsoever since it directly compares them without conversion meaning it's stricter and faster.
2=='2'
true
2==='2'
false

Categories

Resources