JavaScript logoJavaScript/
JS-D013

Invalid `.charAt` comparisonJS-D013

Minor severityMinor
Bug Risk categoryBug Risk

The result of String.prototype.charAt() should not be compared with a string of length 2 or more. Having bad conditions like this will create invalid output.

It is recommended to compare String.prototype.charAt() (example "some string here".charAt(1)) with characters of length only 1 as output of String.prototype.charAt() is always a single character.

Bad Practice

if ('foo'.charAt(1) === 'ab') {
}
// 'ab' length is 2

if ('foo'['charAt'](1) === '/o') {
}

// '/o' length is 2

if ('foo'.charAt(1) === '/o') {
}
if ('foo'.charAt(1) === '
') {
}

//  '
' contains escape character

if ('foo'.charAt(1) === 'a') {
}

// 'a' length is 1