On this page I explain what strings are and then give an overview of some useful things you can do with them.
Strings are simply groups of characters, like ‘JavaScript’, ‘Hello world!’, ‘http://www.quirksmode.org’ or even ‘14′.
When you write JavaScripts, you need to know what strings are and how they work. You’ll use them a lot, since most things you can read out (the URL of a page, a style sheet declaration, the value of a form field) are strings.
First I explain the basics of strings. Then I explain the fuzzy line between strings and numbers in JavaScript. If you have programming experience in another language, please read this part carefully.
Finally I give some of the most important methods and properties of strings.
String basics
Let’s review the basics of strings.
Quotes
When you declare and manipulate strings in JavaScript, always write them with single quotes ‘ or double quotes ” around them. This tells the browser that it’s dealing with a string. Don’t mix up your quotes, if you start a string with a single quote and end it with a double quote, JavaScript doesn’t understand what you mean. As a rule, I use single quotes ‘ because I’ve decided to use double quotes for HTML and single quotes for JavaScript. You can of course do this the other way around, but I advise you to make some such rule for yourself.
Let’s introduce our two test strings that we’ll use throughout this page:
var a = ‘Hello world!’;
var b = ‘I am a JavaScript hacker.’
Now we have declared two variables, a and b and put strings in them. Having done this, we can start working with them. But first of all, a problem: suppose I’d written
var b = ‘I’m a JavaScript hacker.’;
This string has a single quote in it, so JavaScript thinks that the string ends, doesn’t understand what comes next and gives error messages. So you need to escape the single quote, telling the browser to treat the quote as a character, and not as a command to end the string. This is done by placing a backslash \ before it:
var b = ‘I\’m a JavaScript hacker.’
Note that you can put double quotes in the string without escaping them. After all, you’ve defined the single quotes as the beginning and end of the string, so
var b = ‘I\’m a JavaScript “hacker”.’
gives no problems. The double quotes are automatically treated as parts of the string, not as commands.
Pre-written functions
Now that you’ve defined the strings, you can start using them. For instance, you can append one string to another, you can take the second to fourth character of b and put them in the middle of string a, you can read out what the twelfth character of a is, how many characters b has, if there’s a ‘q’ in them and many more things.
To do this, you can use some automatic functions that JavaScript assigns to each string. One of them is .length that gives the length of the string. So if you want to get the length of ‘Hello world!’ you can do:
var c = ‘Hello world!’.length;
But above we put this string in the variable a. Thereby you make a a string, so it also has a length and this yields the same result:
var c = a.length;
The important thing is that you can use .length on any string: it’s an automatic feature. You can read out the length of any string, whether JavaScript makes it for you (like location.href or document.title) or you have declared it yourself.
I give a list of common automatical methods and properties below.
Strings and numbers
JavaScript is very relaxed about the difference between strings and numbers. Some programming languages require you to state if a variable is a number or a string before doing anything else with it. Not so in JavaScript. In fact you can even add up numbers and strings:
var c = a + 12;
Some programming languages would shut down in disgust when they encounter this line. After all, a is a string and 12 is a number. JavaScript, however, tries to solve the problem by assuming 12 is also a string. So c becomes
Hello world!12
So if you use + on a string and a number, JavaScript is going to make the number a string for you. Better still, if you need it you can treat numbers as strings or strings as numbers.
Conversely, if you apply mathematics to a string, JavaScript tries to make it a number. If the string cannot be interpreted as a number (because there are letters in it, for instance), JavaScript gives NaN (Not a Number).
Finally, JavaScript makes no reliable difference between integers and floating point variables.
Number to string
toString() does not work in Netscape 2 and Explorer 3.
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();
Now you can use all methods of strings on d, while c still contains the number.