javascript - Typescript String Comparison Oddity with String.toLowerCase -
while curious (and no js background) i'm beginning dive typescript , face brick wall. want compare 2 strings , make life easy aligned lowercase first. code:
let bool: boolean = false; let = 0; this.comparisons[++i] = " init bool " + " => " + bool; bool = false; if ("a" == "a") { bool = true }; this.comparisons[++i] = ' "a" == "a" ' + " => " + bool; bool = false; if ("a" == "b") { bool = true }; this.comparisons[++i] = ' "a" == "b" ' + " => " + bool; bool = false; if ("a" == "a") { bool = true }; this.comparisons[++i] = ' "a" == "a" ' + " => " + bool; bool = false; if ("a".tolowercase == "a".tolowercase) { bool = true }; this.comparisons[++i] = ' "a".tolowercase == "a".tolowercase ' + " => " + bool; bool = false; if ("a".tolowercase == "b".tolowercase) { bool = true }; this.comparisons[++i] = ' "a".tolowercase == "b".tolowercase ' + " => " + bool;
and prints:
init bool => false "a" == "a" => true "a" == "b" => false "a" == "a" => false "a".tolowercase == "a".tolowercase => true "a".tolowercase == "b".tolowercase => true
why last expression evaluate true?
"a" == "b" should evaluate false third statement.
to call method must use parentheses ()
, when there no arguments pass method:
bool = false; if ("a".tolowercase() == "b".tolowercase()) { bool = true };
or simply:
bool = ("a".tolowercase() == "b".tolowercase());
without parentheses, "a".tolowercase
reference string.tolowercase
method itself. result of comparison true
because compares 2 methods , finds indeed same method.
Comments
Post a Comment