What is the difference between a single and double escape (backslash) in Javascript string?

After attempting to answer the question to this post

Another SO post

I noted that this

Function('return "\\101\\40\\171\\145\\154\\154\\157\\167\\40\\142\\165\\164\\164\\157\\156\\56"')()

or

Function('return "\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"')()

both return the same string - "A yellow button."

In regards to the question I linked to above, the JS code snippet is run in a tag.

I have also noted in the past that when working on some JSON strings, that the '\' was needed over '\'

When is it necessary? Why is it necessary? If it is not necessary, then why is it done?

Links and additional reading are helpful to me.

UPDATE: Perhaps I did not ask this question with enough information.

I understand when and why escaping is needed.

What I would like to know are two things

In the link above, the code has two function; one nested in the other like so

Function(
  Function(
    'return\'\\141\\75\\160\\162\\157\\155\\160\\164\\50\\47\\105\\156\\164\\162\\145\\172\\40\\154\\145\\40\\155\\157\\164\\40\\144\\145\\40\\160\\141\\163\\163\\145\\47\\51\\73\\151\\146\\50\\141\\75\\75\\47\\164\\157\\164\\157\\61\\62\\63\\154\\157\\154\\47\\51\\173\\141\\154\\145\\162\\164\\50\\47\\142\\162\\141\\166\\157\\47\\51\\73\\175\\145\\154\\163\\145\\173\\141\\154\\145\\162\\164\\50\\47\\146\\141\\151\\154\\56\\56\\56\\47\\51\\73\\175\'')())()

This script works as well as this script

Function(
  Function('return "\141\75\160\162\157\155\160\164\50\47\105\156\164\162\145\172\40\154\145\40\155\157\164\40\144\145\40\160\141\163\163\145\47\51\73\151\146\50\141\75\75\47\164\157\164\157\61\62\63\154\157\154\47\51\173\141\154\145\162\164\50\47\142\162\141\166\157\47\51\73\175\145\154\163\145\173\141\154\145\162\164\50\47\146\141\151\154\56\56\56\47\51\73\175"')())()

Why, if both scripts work would the "\\" be needed over "\"? Is there something I am missing here?

The second question would be,

Are there fundamental occurances when "\\" would be required over "\"?

I think sventschui is getting there, but not quite yet.

Answers:

Answer

I think sventschui has the right idea, let me try to explain it in a different way.

When you define a string using str1='"\\101\\40\\171\\145\\154\\154\\157\\167\\40\\142\\165\\164\\164\\157\\156\\56"'

The \\ is the escape sequence and the actual string in memory is (including the quotes around it) "\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"


When you defined a string as str2="\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"

The \101,\40,... are the escape sequences and the actual string in memory is (including the quotes around it)"A yellow button"


When you create a Function(), it re-evaluates the strings (like eval), for the first case, str1 now it treats the \101, \40 as escape sequences, and the returned string is A yellow button, without the quotes around it.

When you do the same thing to the second string, there are no escape sequences, just regular characters, so the string is unchanged (except for the quotes around it)

var str1 = '"\\101"'; // "\101"
var str2 = '"\101"'; //  "A"

var str1Evaled = eval(str1); // \101 is the escape sequence, outputs A
var str2Evaled = eval(str2); // No escape sequence, a raw A

console.log({str1, str2, str1Evaled, str2Evaled});

//  Object {str1: ""\101"", str2: ""A"", str1Evaled: "A", str2Evaled: "A"}

Answer

Because you're nesting two strings both examples return the same string.


'return "\\101\\40\\171\\145\\154\\154\\157\\167\\40\\142\\165\\164\\164\\157\\156\\56"'

evaluates to

return "\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"

what evaluates to

A yellow button

'return "\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"'

evaluates to

return "A yellow button"

what evaluates to

A yellow button

Edit:

Double escaping is used because when \000 WOULD evaluate to \ the following example would mess up:

'return "\000\000"'

evaluates to

return "\\"

what evaluates to

\

'return "\\000\\000"'

evaluates to

return "\000\000"

what evaluates to

\\

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.