javascript - Typescript String Comparison Oddity with String.toLowerCase - Stack Overflow

while curious (and no JS background) I'm beginning to dive into Typescript and face a brick wall.

while curious (and no JS background) I'm beginning to dive into Typescript and face a brick wall. I want to pare two strings and to make life easy they will be aligned to lowercase first. This is the code:

let bool: boolean = false;
let i = 0;
thisparisons[++i] = " init bool " + " => " + bool;

bool = false;
if ("a" == "a") { bool = true };
thisparisons[++i] = ' "a" == "a" ' + " => " + bool;

bool = false;
if ("a" == "b") { bool = true };
thisparisons[++i] = ' "a" == "b" ' + " => " + bool;

bool = false;
if ("a" == "A") { bool = true };
thisparisons[++i] = ' "a" == "A" ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "A".toLowerCase) { bool = true };
thisparisons[++i] = ' "a".toLowerCase == "A".toLowerCase ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "B".toLowerCase) { bool = true };
thisparisons[++i] = ' "a".toLowerCase == "B".toLowerCase ' + " => " + bool;

and it prints:

init bool => false

"a" == "a" => true

"a" == "b" => false

"a" == "A" => false

"a".toLowerCase == "A".toLowerCase => true

"a".toLowerCase == "B".toLowerCase => true

Why does the last expression evaluate to true?

"a" == "b" should evaluate to false like the third statement.

while curious (and no JS background) I'm beginning to dive into Typescript and face a brick wall. I want to pare two strings and to make life easy they will be aligned to lowercase first. This is the code:

let bool: boolean = false;
let i = 0;
this.parisons[++i] = " init bool " + " => " + bool;

bool = false;
if ("a" == "a") { bool = true };
this.parisons[++i] = ' "a" == "a" ' + " => " + bool;

bool = false;
if ("a" == "b") { bool = true };
this.parisons[++i] = ' "a" == "b" ' + " => " + bool;

bool = false;
if ("a" == "A") { bool = true };
this.parisons[++i] = ' "a" == "A" ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "A".toLowerCase) { bool = true };
this.parisons[++i] = ' "a".toLowerCase == "A".toLowerCase ' + " => " + bool;

bool = false;
if ("a".toLowerCase == "B".toLowerCase) { bool = true };
this.parisons[++i] = ' "a".toLowerCase == "B".toLowerCase ' + " => " + bool;

and it prints:

init bool => false

"a" == "a" => true

"a" == "b" => false

"a" == "A" => false

"a".toLowerCase == "A".toLowerCase => true

"a".toLowerCase == "B".toLowerCase => true

Why does the last expression evaluate to true?

"a" == "b" should evaluate to false like the third statement.

Share Improve this question asked Apr 19, 2016 at 20:00 Robert NordenRobert Norden 1001 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

To call a method you must use the parentheses (), even when there are no arguments to pass to the method:

bool = false;
if ("a".toLowerCase() == "B".toLowerCase()) { bool = true };

Or simply:

bool = ("a".toLowerCase() == "B".toLowerCase());

Without the parentheses, "a".toLowerCase is simply a reference to the String.toLowerCase method itself. The result of the parison is true because it pares the two methods and finds that they are indeed the same method.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744836397a4596286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信