-1

My constant declaration is like that:

const table = req.params.table;

And when I send a request with "circle" as table param, table.length returns as 7.

I tried to make this like that:

table.toString().trim().length

But it's still showing that this word's length is 7.

How can I solve this problem?

mlod_
  • 82
  • 7
akasaa
  • 544
  • 2
  • 14

2 Answers2

1

As per what I have observed in the data that's being received there's an extra U+200B zero width space character. That's the main reason why the length is not coming as expected.

I have copied the { table : '​circle' } from the comments and tried to execute the same in browser console, this is how it looks in the console.

enter image description here

In the below snippet also the same thing is happening i.e., \u200b which is zero width space unicode character is being added

let x = { table : '​circle' }

console.log(x.table.length);

In order to avoid this issue you can do something like below

let x = { table: '​circle' }

const { table } = x;
const table2 = table.replace('\u200b', '');

console.log(table.length);
console.log(table2.length);

For more info please refer

Nithish
  • 4,004
  • 2
  • 5
  • 19
0

You need an assigment, because strings are immutable.

 table = table.toString().trim()
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324