Search code examples
javascriptparseint

Strange behaviour when parsing text to int javascript


Alright,

This one is a bit hard to replicate probably, but I cannot find the source of the problem..

When I try to do a parseInt on the test[0], it returns NaN:

var test = ["‎02", "09", "2015 17:34"];
var result = parseInt(test[0], 10); // Return NaN!
var result = parseInt(test[1], 10); // Return 9!
console.log(test[0].length) // Returns 3!! Should be 2

for(var i = 0; i < test[0].length; ++i) {
  console.log(test[0][i]);
}
//Logs: 
// EMPTY LINE
// 0
// 2

I tried to trim it, but that also doesn't work.. I tried it both in Chrome + Firefox, same result..

When someone else tries it, it works!

Rebooted computer, reinstalled Chrome.. Doesn't change a bit..

Any idea's?

Thanks


Solution

  • There is an invisible character in the first string.

    test[0].charCodeAt(0)
    

    returns 8206. When you google for this code it says it's an "left-to-right mark". That's a control character that holds some information but has nothing to display.

    You should also notice that you can delete or select that character without noticing any visual changes. If you delete that character, everything is fine again.