JavaScript string tutorial
In this tutorial you will learn how to use and manipulate strings in JavaScript.
Tutorial info:
| Name: | JavaScript string tutorial |
| Total steps: | 3 |
| Category: | Basics |
| Date: | 2008-02-05 |
| Level: | Beginner |
| Product: | See complete product |
| Viewed: | 10211 |
Bookmark JavaScript string tutorial
Step 1 - JavaScript string basics
JavaScript string tutorial
The string in JavaScript - similar to other programing languages - is a variable which stores a series of characters. A string can be any text inside a quote pair. You can use simple or double quotes, but of course the pairs must match. The next code example shows how you can define strings in JavaScript:
Code:
var str1 = 'Test string 1'; var str2 = "Test string 2"; var str3 = 'It\'s a string'; var str4 = "This is an 'internal' string"; var str5 = 'This is also an "internal" string';
As you can see in case of str3 we used a single quote to define our string content, but the text itself also contains a single quote. In such cases you need to use a backslash character before the single quote to display your text properly.
Concatenating strings
You can also create a string by concatenating more strings together. This could be very useful if you build up your string within an application logic. To do this you have 2 operators to use. The + and the +=. The example below shows you how to use them.
Code:
var str1 = 'This ' + 'is ' + 'a ' + 'demo!'; var str2 = 'Part 1'; str2 += ' and Part 2'; var str3 = 'Mixed '; str3 += 'mode ' + 'is possible.';
In the next section I will show you how to use the most basic string manipulation functions.
Next Step of JavaScript string tutorial
Tags: javascript string functions, javascript string, javascript, string, function
| JavaScript string tutorial - Table of contents |
|---|
| Step 1 - JavaScript string basics |
| Step 2 - String manipulation with JavaScript |
| Step 3 - More string manipulation functions |