String.raw can be used to create a string that contains backslashes, without having to double up those backslashes.
Historically, you'd need to double up backslashes when creating a string:
let str = "C:\\Program Files\\7-Zip";
console.log(str);
String.raw allows your code to show the path without doubled backslashes:
let str = String.raw`C:\Program Files\7-Zip`;
console.log(str);
The above code works fine, but today I discovered that it doesn't work if the raw string ends with a backslash:
let str = String.raw`Can't End Raw With Backslash\`;
console.log(str);
The above snippet produces this error:
{
"message": "SyntaxError: `` literal not terminated before end of script",
"filename": "https://stacksnippets.net/js",
"lineno": 14,
"colno": 4
}
Why is this an exception?
Update: Another example where the single backslash doesn't do what I'd like is:
let str1 = "folder";
let str2 = "subfolder";
let fileName = "file.txt"
let path = String.raw`\${str1}\${str2}\${fileName}`;
console.log(path);
While I was hoping for this output:
\folder\subfolder\file.txt
Instead it outputs:
\${str1}\${str2}\${fileName}
The backslash escapes the dollar sign's special meaning. So, this is yet another circumstance where you still have to double up those backslashes :(