diff --git a/blog_images/glass-blowing.jpg b/blog_images/glass-blowing.jpg new file mode 100644 index 000000000..d032e6b85 Binary files /dev/null and b/blog_images/glass-blowing.jpg differ diff --git a/blog_posts/javascript-prevent-string-being-escaped.md b/blog_posts/javascript-prevent-string-being-escaped.md new file mode 100644 index 000000000..4bb5fb0b6 --- /dev/null +++ b/blog_posts/javascript-prevent-string-being-escaped.md @@ -0,0 +1,18 @@ +--- +title: "Tip: Prevent a string from being escaped in JavaScript" +type: tip +tags: javascript,string +authors: chalarangelo +cover: blog_images/glass-blowing.jpg +excerpt: Strings in JavaScript can be escaped in various ways. But what if you need to prevent a string from being escaped? Here's a handy trick for that. +--- + +By default, when JavaScript sees an escape character (`\`), it will escape the character after it. However, there are cases where you might not want this behavior (e.g. when you want to store a Windows path as a string). For these cases, you can use a template literal and the `String.raw()` tag function: + +```js +const path = `C:\web\index.html`; // 'C:web.html' + +const unescapedPath = String.raw`C:\web\index.html`; // 'C:\web\index.html' +``` + +**Image credit:** [Kym MacKinnon](https://unsplash.com/@vixenly?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)