#5 JavaScript String

What do we discuss in this article?

  1. The String Type
  2. Character Literals
  3. String Conversions
  4. The String() Function

A] The String Type

  • A sequence of characters [16 bit Unicode characters to be precise] is called a String in JavaScript. Strings can be enclosed within double quotes or single quotes in JavaScript.
  •  Both the strings declared above are perfectly valid despite the fact that they use different quotation marks.
  • A string using double quotes is exactly the same as a string using single quotes.
  • A string beginning with a double quote must end with a double quote, and a string beginning with a single quote must end with a single quote, otherwise it causes a syntax error.

 B] Character Literals

  • Certain characters which cannot be printed directly like a tab or a new line are printed by putting a backslash in front of that character. Here’s a list of special characters also known as escape sequences.
Literal What it does? Example How it looks?
\n Prints New Line var message=”Hello \nFrom \nVivz”;
javascript new line

slidenerd.com

\t Prints Tab var message=”Hello\tVivz”;
javascript tab

slidenerd.com

\b Prints BackSpace var message=”Hello\bVivz”;
javascript backspace

slidenerd.com

\r Prints carriage return var message=”Hello \r Vivz”; Same as newline on most browsers
\f Prints form feed[Used to tell a printer to stop wherever it is printing currently and start from a new page] var message=”Hello \f Vivz”; Prints a newline depending on browsers
\\ Prints backslash within the String var message=” Hello \\ Vivz”;
javascript backslash

slidenerd.com

\’ Print a single quote on the screen var message=”\’hey\’ how are you?”
javascript display single quote

slidenerd.com

\” Print a double quote on the screen var message=”\”hey\” how are you?”
javascript display double quote

slidenerd.com

\x Lets you print the hex value corresponding to an ASCII value that you want to show on the screen var message=”\x41 mango”;The above form is equivalent to \xnn where n is a hex digit between 0 and F and 41 corresponds to 65 which is the ASCII value of A
javascript \xnn

slidenerd.com

\unnnn Each n is a hex digit between 0-F, This lets you print Unicode special characters var message=”\u03a3 sum”;\u03a3 is the ASCII symbol for the special Greek character known commonly as ‘summation’
javascript \uxxxx

slidenerd.com

  • These character literals can be included anywhere with a string and will be interpreted as if they were a single character.
  •  The length of the statement above is shown as 5 even though we have \u03a3 itself making 6 characters. This is because the entire escape sequence including the backslash is treated as a single character when displayed.
javascript escape sequence example

slidenerd.com

  • The length of any string can be returned by using the length property shown in the above example.
  • This property returns the number of 16-bit characters in the string.

C] String Conversions

  • Once Strings are created, their values cannot change.
  • To change the string held by a variable, the original string must be destroyed and the variable filled with another string containing a new value.
  • There are two ways to convert a value into a string.
    1. Use the toString() method that almost every value has.
    2. Add an empty String to that value with the ‘+’ operator
  •  The toString() method is available on values that are numbers, Booleans, objects, and strings but not available if the value is null or undefined.

Converting Numbers To Different Base Using toString()

  • When used on a variable containing some number value, toString() actually accepts a single argument which is the radix in which to output the number.
  • By default, toString() always returns a string that represents the number as a decimal, but by passing in a radix, toString() can output the value in binary, octal, hexadecimal, or any other valid base.
  • The default output of a variable containing a number when converted to a String using the toString() is the same as supplying the radix 10 asking toString() to print a decimal notation of that number.
  • In simple words, by default toString()==toString(10) for a number.

D] The String() Function

  • If you’re not sure that a value isn’t null or undefined, you can use the String() casting function, which always returns a string regardless of the value type.
  • The String() function follows the rules specified below
    1. If the value has a toString() method, it is called (with no arguments) and the result is returned.
    2. If the value is null, “null” is returned.
    3. If the value is undefined, “undefined” is returned.
  •  A variable with a value null is converted to “null” whereas undefined gets converted to “undefined”