Home > Back-end >  How these two strings aren't equal
How these two strings aren't equal

Time:11-25

I am trying for an hour's and not getting the satisfied output.

Below in the code, the first variable db fetching the value from database. And, the second variable api sending the value into api request.

When I comparing these two variables, getting the not equal output. I don't know why?

Please give your suggestions.

One more thing, from the next time How do I compare these two values using which methods.

const db = 'Sales E-mail'
const api = 'Sales E-mail'
console.log(db==api?'equal':'not equal')

console.log(api.length,db.length)
console.log(typeof api,typeof db)

CodePudding user response:

There must be some different hidden character between the two that makes it evaluate it as false. If you copy paste the values it evaluates as true.

const db = 'Sales E-mail'
const api = 'Sales E-mail' // copy & pasted from the db
console.log(db==api?'equal':'not equal')

console.log(api.length,db.length)
console.log(typeof api,typeof db)

CodePudding user response:

The issue seems to be in the space character, if you remove it then the comparison works, maybe the first string, coming from the db has a 'different' space. you can fix it in this way:

const db = 'Sales E-mail'.replace(' ', ' ');
const api = 'Sales E-mail'.replace(' ', ' ');

CodePudding user response:

You can see on the image bellow, that the space between words is not same character, the second one is space, the first one is some character that is not space (orange dot represents the space), which is missing at first string.

enter image description here

Snippet of character code of spaces:

const db = 'Sales E-mail'
const api = 'Sales E-mail'
console.log(db==api?'equal':'not equal')

console.log(api.length,db.length)
console.log(typeof api,typeof db)
console.log("First space:", " ".charCodeAt(0));
console.log("Second space:", " ".charCodeAt(0));

CodePudding user response:

It's Equal. Please Change Compiler https://www.programiz.com/javascript/online-compiler/

  • Related