What is the correct way to check for String equality in Typescript (for Angular 4)
For simple string-to-string equality, you can use the ===
operator.
Comparing Strings in an IF statement
The reason why this was happening was because the text response from my XMLHttpRequest object was actually "GB " - three characters long.
I just cut it down to two and it worked fine:
cc.substring(0,2)
Which equals operator (== vs ===) should be used in JavaScript comparisons?
The strict equality operator (===
) behaves identically to the abstract equality operator (==
) except no type conversion is done, and the types must be the same to be considered equal.
Reference: Javascript Tutorial: Comparison Operators
The ==
operator will compare for equality after doing any necessary type conversions. The ===
operator will not do the conversion, so if two values are not the same type ===
will simply return false
. Both are equally quick.
To quote Douglas Crockford's excellent JavaScript: The Good Parts,
JavaScript has two sets of equality operators:
===
and!==
, and their evil twins==
and!=
. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then===
producestrue
and!==
producesfalse
. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use
===
and!==
. All of the comparisons just shown producefalse
with the===
operator.
Update:
A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning objects. For objects, ==
and ===
act consistently with one another (except in a special case).
var a = [1,2,3];
var b = [1,2,3];
var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };
var e = "text";
var f = "te" + "xt";
a == b // false
a === b // false
c == d // false
c === d // false
e == f // true
e === f // true
The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its toString
or valueOf
method. For example, consider the comparison of a string primitive with a string object created using the String
constructor.
"abc" == new String("abc") // true
"abc" === new String("abc") // false
Here the ==
operator is checking the values of the two objects and returning true
, but the ===
is seeing that they're not the same type and returning false
. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the String
constructor to create string objects from string literals.
Reference
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
Optimum way to compare strings in JavaScript?
You can use the localeCompare()
method.
string_a.localeCompare(string_b);
/* Expected Returns:
0: exact match
-1: string_a < string_b
1: string_a > string_b
*/
Further Reading:
- MDN: String.prototype.localeCompare
- Stack Overflow - Is there a JavaScript strcmp()?
- Tutorials Point: JavaScript String - localeCompare() Method
Typescript - correct way to set a string based on string comparison
Consider using a ternary expression so you’re not declaring the variable inside the conditional:
const path:string = myString === “someString” ? “option a” : “option b”
is there a way to compare two strings without converting the Case?
You can use localCompare()
which supports case insensitive comparisons.
const string1 = "My dear friend"
var string2 = "my dEar frienD"
console.log(string1.localeCompare(string2, undefined, { sensitivity: 'base' }) === 0)
Related Topics
How to Validate Date with Format "Mm/Dd/Yyyy" in JavaScript
How to Sort an Array of Objects with Jquery or JavaScript
Getelementsbyclassname() Doesn't Work in Old Internet Explorers Like IE6, IE7, IE8
Are All JavaScript Callbacks Asynchronous? If Not, How to Know Which Are
Looping Through Localstorage in HTML5 and JavaScript
Angularjs Does Not Load Scripts Within Ng-View
How to Auto-Scroll to End of Div When Data Is Added
Sort an HTML List with JavaScript
What Benefits Does Es2015 (Es6) 'Class' Syntax Provide
How to Fix Navigator/Window/Document Is Undefined in Nuxt
Calling a JavaScript Function Returned from an Ajax Response
How to Detect Whether an Iframe Is Loaded
Use Functions Defined in Es6 Module Directly in HTML