Files
30-seconds-of-code/blog_posts/javascript-prevent-string-being-escaped.md
2022-12-04 22:26:44 +02:00

876 B

title, shortTitle, type, tags, author, cover, excerpt, firstSeen, lastUpdated
title shortTitle type tags author cover excerpt firstSeen lastUpdated
Tip: Prevent a string from being escaped in JavaScript Prevent string escaping tip javascript,string chalarangelo blog_images/glass-blowing.jpg 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. 2021-06-12T19:30:41+03:00 2021-06-17T12:00:00+03:00

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:

const path = `C:\web\index.html`; // 'C:web.html'

const unescapedPath = String.raw`C:\web\index.html`; // 'C:\web\index.html'