swissmisfit

Javascript Strings, Properties, Special Characters and Escape Sequences

Javascript Escape Sequence Backslash

Javascript Strings, Properties, Special Characters and Escape Sequences

Javascript strings are used for storing sequences of characters. Javascript strings are enclosed within delimiters, most commonly, single quotes, ‘ ‘, or double quotes, “ “. Text within delimiters is commonly referred to as string literals, where literal means that the value is a fixed value within the source code. You can think of literals as the exact opposite of a variable. Variables can take many values depending on the circumstances, while literals are ‘fixed’ on a particular value.

’string literal within single quotes’
“string literal with double quotes"

Length Property

Javascript comes built with a length property for strings. The length property returns the count of characters found within a string. A value of 0 will be returned for all empty strings. In the below example, a value of 6 will be returned.

Length Example

var string = "Hello!"
string.length
> 6

Escape Sequences

In addition to string literals and regular printable characters, Javascript is able to produce special characters using escape notation or escape sequences. Escape sequences begin with a backslash or escape character. The escape character ( \ ) can be used to insert apostrophes, new lines, quotes, and other special characters into a string.

The table below lists other special characters that can be added to a text string with the backslash sign:

CodeOutput
\’ single quote
\” double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace

Escape Sequence Examples

In order to familiarize yourself with these commands take a look at the following examples oh how \”, \\, \n, \t are used to manipulate output with escape sequences.

“I’m kind of a \”big deal\” around here.”
> I’m kind of a “big deal” around here.
“The file can be found on c:\\Desktop\\Files\\Document.txt”
> The file can be found on c:\Desktop\Files\Document.txt
”This is the \n beginning of the next line.”
> This is the
> beginning of the next line.
“My favorite movies include: \n \t Dark Knight Series \n \t Iron Man”
> My favorite movies include:
>     Dark Knight Series
>     Iron Man

That is about it for string properties and escape sequences. Now to take it a step further and call methods on strings to begin manipulating them. Check out my post about JavaScript string search methods to begin exploring how to work with strings.

Tagged with: ,
Posted in Web Development

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Recent Projects
  • Grub.ly
    Explore and discover new foods!
    Comments

  • Grub.ly
    Explore and discover new foods!
    Comments

About Me Dal Price: SwissMisfit

I’m Dal, an enthusiast for all things technology, design, music, and food. This is a collection of inspirations, misfit thoughts, and random musings that may or may not be interesting to others.

Other Places to find me
  • RSS
    Feed updates from the blog

  • Twitter
    Bits of random 140 characters

  • Flickr
    So many pruty pictures!

  • XBox
    Pwning N00bs

  • Instagram
    Filters make my pictures better