0

var i = 'Hello \n World'
console.log(i)   

/*
Output:
Hello
World
*/

/*
Desired output: Hello \n world
*/

var j = 'javscr\u0012ipt'
console.log(j)

/*
Output:
javscr ipt
*/

/*
Desired output: javscr\u0012ipt
*/

Above code shows what output I am supposed to get when i have a special character in the string, what output I want. I don't want the special character whether it is line break or unicode to get rendered on screen and rather they should be a part of string. Can anyone tell me how can I achieve this. Any help will be appreciated ( I am using angularjs as framework) .

aryan
  • 43
  • 3
  • 1
    You can just add another forward slash \ to escape the default \n , so console.log("Hello \\n World") will give you your desired output – Adam Ma Mar 03 '22 at 08:02

1 Answers1

0

you can transform your string as json one to display special character

var i = 'Hello \n World'
console.log(JSON.stringify(i));
var j = 'javscr\u0012ipt'
console.log(JSON.stringify(j));
jeremy-denis
  • 6,368
  • 3
  • 18
  • 35