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

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -