Tuesday, June 26, 2012

Java Script Tutorial 19 : JavaScript Special Characters


In JavaScript, Special characters can be added to a text string by using the backslash sign. For example we want to display following text in JavaScript code. 


var txt="This "This is a "big" Apple.";


In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to: 


This is a  


We must place a backslash (\) before each double quote in "big" to solve this problem. This result string will look like this :


var txt="This "This is a \"big\" Apple.";


After executing in browser, it will display string as : 


This is a "big" Apple. 


There are many other special characters that can be added to a text string with the backslash sign, these are :


\'          (  single quote  )
\"         (  double quote  )
\\          (  backslash  )
\n         (  new line  )
\r          (  carriage return  )
\t          (  tab  )
\b         (  backspace)
\f          (  form feed  )

No comments:

Post a Comment