Reputation: 7115
In Javascript, why does this statement not equal '\b,\b'
?
['\b', '\b'].join()
//=> ","
According to MDN docs on join
:
If an element is undefined or null, it is converted to the empty string.
So why is \b
being evaluated as undefined/null?
Additionally, the\b
is dropped from any string prepended with it, e.g:
['\btest', '\btest2'].join()
//=> "test,test2"
Something crazy is going on.
Upvotes: 1
Views: 240
Reputation: 68413
So why is \b being evaluated as undefined/null?
As per the spec
In determining the sequence any occurrences of \ UnicodeEscapeSequence are first replaced with the code point represented by the UnicodeEscapeSequence and then the code points of the entire IdentifierName are converted to code units by UTF16Encoding (10.1.1) each code point.
Also read this part to understand which are escape character and which are not
CharacterEscapeSequence ::
SingleEscapeCharacter NonEscapeCharacter
SingleEscapeCharacter ::
one of ' " \ b f n r t v
NonEscapeCharacter
:: SourceCharacter but not one of EscapeCharacter or LineTerminator
EscapeCharacter ::
SingleEscapeCharacter
DecimalDigit x u
HexEscapeSequence ::
x HexDigit HexDigit UnicodeEscapeSequence :: u Hex4Digits u{ HexDigits }
Which is why \b
being a special character is removed while \a
is still "a".
Upvotes: 2
Reputation: 971
\b
is a special character, which means backspace.
That is why it is being converted to the 'empty' string.
Upvotes: 1