hasankzl
hasankzl

Reputation: 1019

javascript equality operators why return false after to toLowerCase() even the strings are same (utf-8)?

Hi I'm trying to run this code in javascript

const upperCase = "İ"
const lowerCase = "i"
const newLowerCase=upperCase.toLowerCase();

console.log(lowerCase);
console.log(newLowerCase);
// expected output: true, return false
console.log(newLowerCase == lowerCase);

this is returning true

console.log("i".includes("i"))

but this is also returning true

console.log("İ".toLowerCase().includes("i"))

This must be about characters in Turkish Language but I couldn't figure it out why this happening. How can I fix this problem?

Upvotes: 1

Views: 180

Answers (4)

hasankzl
hasankzl

Reputation: 1019

to solved this use toLocaleLowerCase() function.

const upperCase = "İ"
const lowerCase = "i"
const newLowerCase=upperCase.toLocaleLowerCase('tr-TR');

console.log(lowerCase);
console.log(newLowerCase);
// expected output: true, return true
console.log(newLowerCase == lowerCase);

if somehow does not work you can also use this

    const upperCase = "İ"
    const lowerCase = "i"
    const newLowerCase=upperCase.replace(/İ/g,"i").toLowerCase();

    console.log(lowerCase);
    console.log(newLowerCase);
    // expected output: true, return true
    console.log(newLowerCase == lowerCase);

Upvotes: 2

cyrille
cyrille

Reputation: 2659

you are using a character which is not the uppercase I, but a unicode character. If you try the following that will explain why the == operator returns false

"İ".charCodeAt(0) => 304 "I".charCodeAt(0) => 73

304 is different from 73

"İ".toLowerCase().length => 2 "I".toLowerCase().length => 1

results have not the same length

"İ".toLowerCase().charCodeAt() => 105 (which is lowercase i) "İ".toLowerCase().charCodeAt(1) => 775

The additional character is the lowercase of the dot above you had - https://www.codetable.net/decimal/775

Upvotes: 0

Alnitak
Alnitak

Reputation: 339927

The output of "İ".toLowerCase() is a lower case i with an additional \u0307 "combining dot above".

That is, the result is \u0069\u0307.

This string therefore does include the lower case i, hence the true result for .include, but it does not equal a lower case i.

Upvotes: 2

edjm
edjm

Reputation: 5522

Looks like some kind of translation problem with regards to the I looking character.

false: console.log("YEŞİL".toLowerCase().includes("yeşil"))
true:  console.log("YEŞL".toLowerCase().includes("yeşl"))

Upvotes: 0

Related Questions