Words are very important to humans — they are a large part of how we communicate. Since the Web is a largely text-based medium designed to allow humans to communicate and share information, it is useful for us to have control over the words that appear on it. HTML provides structure and meaning to our text, CSS allows us to precisely style it, and JavaScript contains a number of features for manipulating strings, creating custom welcome messages and prompts, showing the right text labels when needed, sorting terms into the desired order, and much more.
Pretty much all of the programs we’ve shown you so far in the course have involved some string manipulation.
Strings are dealt with similarly to numbers at first glance, but when you dig deeper you’ll start to see some notable differences. Let’s start by entering some basic lines into the browser developer console to familiarize ourselves.
let string = 'The revolution will not be televised.'; string;
Just like we did with numbers, we are declaring a variable, initializing it with a string value, and then returning the value. The only difference here is that when writing a string, you need to surround the value with quotes.
let badString = This is a test; let badString = 'This is a test; let badString = This is a test';
These lines don’t work because any text without quotes around it is assumed to be a variable name, property name, a reserved word, or similar. If the browser can’t find it, then an error is raised (e.g. “missing; before statement”). If the browser can see where a string starts, but can’t find the end of the string, as indicated by the 2nd quote, it complains with an error (with “unterminated string literal”). If your program is raising such errors, then go back and check all your strings to make sure you have no missing quote marks.
string
— try it now:
let badString = string; badString;
badString
is now set to have the same value as string
.
let sgl = 'Single quotes.'; let dbl = "Double quotes"; sgl; dbl;
let badQuotes = 'What on earth?";
let sglDbl = 'Would you eat a "fish supper"?'; let dblSgl = "I'm feeling blue."; sglDbl; dblSgl;
let bigmouth = 'I've got no right to take my place...';
This leads us very nicely into our next subject.
To fix our previous problem code line, we need to escape the problem quote mark. Escaping characters means that we do something to them to make sure they are recognized as text, not part of the code. In JavaScript, we do this by putting a backslash just before the character. Try this:
let bigmouth = 'I\'ve got no right to take my place...'; bigmouth;
This works fine. You can escape other characters in the same way, e.g. \"
, and there are some special codes besides. See Escape notation for more details.
let one = 'Hello, '; let two = 'how are you?'; let joined = one + two; joined;
The result of this is a variable called joined
, which contains the value “Hello, how are you?”.
+
between each pair. Try this:
let multiple = one + one + one + one + two; multiple;
let response = one + 'I am fine — ' + two; response;
Note: When you enter an actual string in your code, enclosed in single or double quotes, it is called a string literal.
Let’s have a look at concatenation being used in action — here’s an example from earlier in the course:
<button>Press me</button> const button = document.querySelector('button'); button.onclick = function() { let name = prompt('What is your name?'); alert('Hello ' + name + ', nice to see you!'); }
Here we’re using a window.prompt()
function in line 4, which asks the user to answer a question via a popup dialog box then stores the text they enter inside a given variable — in this case name
. We then use a window.alert()
function in line 5 to display another popup containing a string we’ve assembled from two string literals and the name
variable, via concatenation.
'Front ' + 242;
You might expect this to return an error, but it works just fine. Trying to represent a string as a number doesn’t really make sense, but representing a number as a string does, so the browser rather cleverly converts the number to a string and concatenates the two strings.
typeof
operator to check whether the variable is a number or a string):
let myDate = '19' + '67'; typeof myDate;
Number
object converts anything passed to it into a number, if it can. Try the following:
let myString = '123'; let myNum = Number(myString); typeof myNum;
toString() that converts it to the equivalent string. Try this:
let myNum = 123; let myString = myNum.toString(); typeof myString;
These constructs can be really useful in some situations. For example, if a user enters a number into a form’s text field, it’s a string. However, if you want to add this number to something, you’ll need it to be a number, so you could pass it through Number()
to handle this. We did exactly this in our Number Guessing Game, in line 54.
So that’s the very basics of strings covered in JavaScript. In the next article, we’ll build on this, looking at some of the built-in methods available to strings in JavaScript and how we can use them to manipulate our strings into just the form we want.