Check out a free preview of the full The Good Parts of JavaScript and the Web course:
The "Booleans and Strings" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:

After briefly talking about the Booleans, Doug dives into the strings. Strings are immutable sequences of 0 or more 16-bit Unicode characters. Doug outlines some methods in the API and shares a few style suggestions when using strings.

Get Unlimited Access Now

Transcript from the "Booleans and Strings" Lesson

>> [MUSIC]

>> Douglas: So Booleans, there are exactly two Booleans in this language and they are true and false. So hooray, we got that right. We have strings. Does anybody know why they're called strings? Anyone?
>> Speaker 2: The change of characters.
>> Douglas: It turns out nobody knows.
>> Speaker 2: Okay.
>> Douglas: The first place I've been able to find in the literature that refers to a specific data type, which is a sequence of characters, is the ALGOL 60 report.

[00:00:35] But there's nothing in that report which explains why they chose the word string. The first time you encountered it you must have thought, well this is strange cuz it doesn't look like a piece of string any way. So how'd that happen? I asked John McCarthy who was on that committee, he was the inventor of Lisp, why did he call it strings?

[00:00:54] He said, well Clanet was doing this stuff with strings of symbols. But that didn't really answer the question because at the time of the ALGOL 60 report they would talk about a block was a string of statements. So when did it become specifically a string of characters? And he was really annoyed that I was asking a question he didn't know the answer to.

[00:01:20] So that was the end of that conversation and then he died and he was a last survivor of that of the Lisp committee so we'll never know, or of the ALGOL committee. So anyway we got them, we call them strings and I think we call them strings because there may have been some CPU architecture which linked characters together to make string sequences of characters.

[00:01:49] And I think that because what's the operation we do on strings? It's concatenate, but concatenate means to form links, it's an operation in chain making. So you would think if it's strings it should be tie or maybe slice or wind or something that strings do. So we've got some mixed metaphors going on.

[00:02:09] And so I'm still searching the literature trying to find out where strings came from but I haven't found it yet. So anyway we got them, we got strings. A string is a sequence of 0 or more 16-bit Unicode characters. It's characters in the sense of UCS-2 not UTF-16 because there's no awareness of surrogate pairs.

[00:02:31] At the time that JavaScript and Java were designed Unicode was gonna be a 16 bit character set. It later grew to a 21 bit character set and JavaScript is not aware of that. There is no separate character type. Characters are represented as strings with a length of one.

[00:02:49] Strings are immutable which is good. Once you make a string, it cannot be changed, it's fixed for all time. You can make new strings by concatenating together bits of old strings but you cannot change a string once it's made. Similar strings are equal which is great. Java got that wrong, JavaScript got that right, hooray for JavaScript.

[00:03:09] String literals can use single quotes or double quotes with escapement. Both work exactly the same way. No magic quotes or quasi quotes or anything like that. But there's no reason to have two ways to make strings but we do. So given that I recommend use double quote for external strings.

[00:03:27] Strings that make sense outside of the program like URLs and templates and notes to the user, that kind of stuff. And use single quotes for internal strings like the names of properties and character constants and stuff like that. There's nothing that enforces that but I think it's a good convention.

[00:03:49] We can convert a number to a string by using its toString method. I prefer instead to use the global String function which does the same thing, and will also work better on things that do not have toString methods. We can convert a string into a number by using the global Number function or by using the plus prefix operator which I prefer.

[00:04:16] We also have a parseInt function that I don't like. parseInt is something that was borrowed from Java. It will convert a value into a number but it stops parsing at the first non-digit character. And it won't tell you where it stopped, or why it stopped, or what was left over.

[00:04:35] And usually wanna know that cuz that can be important but it doesn't give you any clue. There was a big design error in that if the string you are parsing starts with a 0, it assumes that you meant that to be parsed as base-8, octal. And so if you're parsing something like a date or a time which might have an 08 or an 09 that will be parsed as a 0 because it sees the 0, goes into base-8, it sees the 8, says you are not an octal digit so I stop here and get the 0.

[00:05:09] Yeah, so because of that I recommend you always include the radix argument, which says base-10. We're on planet Earth, damn it, we got base-10, 0 to 9.
>> Douglas: Strings have a length property which tells you how many 16-bit characters are in the string. Surrogate pairs will be counted as two characters in that count.

[00:05:36] Strings are objects and so they contain a big mess of methods, let you look them up offline, but there are a lot of them. Many of them are actually very useful. Every string inherits from string.prototype. So if we wanted to add new methods to strings this is the place where we could do that.

[00:05:56] Again, this is not something applications can do but this is how we've been growing the language.