Using Javascript strings

Using Javascript strings.

  • A string is a sequence of Unicode letters, digits, punctuation characters, and so on.
  • Strings must be typed on one line surrounded by single(') or double(") quotes.
    <script type="text/javascript">
      var empty="";  // This is an empty string with no characters
      var prop='name="myform"'; // variable, prop, contains name='myform' as a string
    </script>
    <!-- used in a HTML file;-->
    <input type='button' value='Show prop value' onclick="alert(prop)">
    <input type='button' value='Click Me' onclick="alert('You clicked me')">
  • You can also include escape sequences in your string.
    var mycat="You\'re right, it can\'t be a cat";
  • Possible Javascript escape sequences:
    Sequence Character represented
    \0 The NUL character (\u0000).
    \b Backspace (\u0008).
    \t Horizontal tab (\u0009).
    \n Newline (\u000A).
    \v Vertical tab (\u000B).
    \f Form feed (\u000C).
    \r Carriage return (\u000D).
    \" Double quote (\u0022).
    \' Apostrophe or single quote (\u0027).
    \\ Backslash (\u005C).
    \xXX The Latin-1 character specified by the two hexadecimal digits XX.
    \uXXXX The Unicode character specified by the four hexadecimal digits XXXX.
    \XXX The Latin-1 character specified by the octal digits XXX, between 1 and 377. Not supported by ECMAScript v3; do not use this escape sequence.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.