-1

Is there a way to use the character '\' inside a string and have it be read as text?

Can't seem to find an answer for this anywhere.

4 Answers4

2

Backslash(\) is an escape character, & to use backslash you need to escape it using another backslash.\\ will output only single \

document.getElementById("demo").innerHTML = "hello\\"
<div id="demo"></div>
brk
  • 48,835
  • 10
  • 56
  • 78
0
console.log('\\');

This will work.

misss-popcorn
  • 591
  • 2
  • 12
0

In ES6 you can use it

let str = `\ Hello World \`;
Farhan Yaseen
  • 2,507
  • 2
  • 22
  • 37
0

just use double backslash(\\) . As single backslash (\)is a reserve word in JavaScript for escape character.

Avee
  • 45
  • 10