-3
I really want to understand why in Javascript this code gives true
.
I couldn’t understand that logic, can someone please explain to me?
console.log(`\`` === '`');
-3
I really want to understand why in Javascript this code gives true
.
I couldn’t understand that logic, can someone please explain to me?
console.log(`\`` === '`');
8
Gives true
because both expressions generate the same string (in this case, it is a string containing only the character `
). Behold:
console.log(`\``); // `
console.log('`'); // `
In the first case, the character `
is the delimiter of a string template, then all that is between the `
is part of the string. For example, `abc`
generates the string "abc".
But what if I want the character itself `
is part of this string? So that it is not confused with the delimiter, it is necessary to escape with \
. That is, when the sequence is found \`
, Javascript understands that this `
is not the string delimiter, but the character itself `
.
In the second case, it is a normal string, delimited by single quotes. Thus, the `
can be written directly inside the quotes, without the need for escape, because there is no way it can be confused with the delimiter (since the delimiters in this case are the quotes).
And in the end, both generate the same string, containing only the character `
.
In addition, the same is true if I want to have quotation marks on a string delimited by quotation marks - for example, '\''
to generate a string containing the character '
. In general, if I want the delimiter to be part of the string, I have to use the escape with \
so that the character is not confused with the delimiter itself.
5
The backslash is just an escape character, that is, then comes a special character, in practice there is only one character there, the bar is only syntax to indicate the differentiation.
It is not any character that can be escaped. Those who can are:
If you want the bar to be considered as a character to be included in the text and not used as an escape then you should use \\
which indicates that the next backslash should be a text bar even.
If you did `
what would he interpret? He would open the text template, then close and open again without closing. It’s not what you want. With the \`
, you open the text, has a character of backtick which is the same as opening and closing, but as it is escaped it counts as the normal character and not as closing, and in the end, it closes the text template as expected.
The second text is a string normal then opening and closing is different from backtick, then there is no ambiguity and no need for the escape.
So the first actually in memory is just a character of backtick, exactly what is in the other text, therefore they are equal.
1
Not just the string template
(`\`` === '`') && ('\`' === '`')
also results in the same, since the backslash function is for encoding special characteres or some sequence like \n b, and ` will be evaluated to `
Browser other questions tagged javascript string
You are not signed in. Login or sign up in order to post.
The question I have before reading the answers is "why shouldn’t it be?"
– Bacco