The code '\'
is not a valid Javascript code. I doubt that the problem you describe is that "this condition is not working". You probably got an error like this:
SyntaxError: Invalid or unexpected token
The reason is that the character \
is not understood to be the literal character "backwards slash" because it is used in Javascript as the "escape character" - it is never understood as is and instead causes the next character to be interpreted as something else.
The sequence "\'
" is understood in Javascript as "the literal single quote character" instead of "'
" which is normally used to start and end single quoted strings.
So when Javascript sees '\'
it sees a start of a single quoted string, followed by the literal "single quote" character, and then ... where's the end of the string?
What you probably meant to have instead is "a string that contains a single literal back slash character". You can do that by disabling the special meaning of \
by using another \
(escaping the escape character).
So your condition should look like this:
if (lastcharacter === '\\') {
BTW: the ===
operator is redundant here, and ==
could have been safely used instead.