Mark As Completed Discussion

In this lesson, we will discuss one of the basic types in programming languages, with a focus on the following key points:

  1. What are strings and common string operations?
  2. Working with strings in JavaScript.

For the Python version of this lesson, please click here.

In the previous lessons, we learnt about variables and their usage. In programming languages, these variables can be assigned values of different types. These include integers, decimal numbers, sequence of alphabetical letters, booleans (true or false) among several others. Programming languages not only offer these data types, but also allow programmers to perform various operations on them. In this lesson, we will focus on the data type called string, and the operations that are defined on strings.

Strings

Most programming languages have a data type called a string, which denotes a sequence of characters, such as Hello World!. A string can contain any characters, be it a number, alphabet, or a special symbol. In most programming languages, strings are declared by enclosing the characters within single ('') or double quotation marks (""). These strings can be assigned to variables, which can then be used for further manipulation using string properties and methods.

Strings can also be defined as a sequence which are made up of many unicode characters, or in other words, they are an array of characters. Each character in a string can be accessed, and different programming languages provide different methods to perform this operation, as we'll see below.

Now let's have a closer look at strings in JavaScript.

Strings in JavaScript

As mentioned above, strings in most programming languages can be declared by single or double quotation marks, and so is the case with JavaScript. Following code block has few examples.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Indexing of Strings

Before we move to string operations, an important concept to discuss regarding strings is indexing. It facilitates the fast lookup of characters within a string by specifying character positions. Suppose we wanted to access the character r from the string in code block below. The position of r in the string is 2. This is because the indices, or the positions in a string start from 0, and not 1, as illustrated below.

JAVASCRIPT
1var name = "Chris"
Indexing of Strings

Are you sure you're getting this? Is this statement true or false?

Does the following code block declare a valid string?

JAVASCRIPT
1var country = "India";

Press true if you believe the statement is correct, or false otherwise.

Are you sure you're getting this? Is this statement true or false?

Strings cannot have capital letters.

Press true if you believe the statement is correct, or false otherwise.

Operations on Strings

In programming languages, operations are defined on different data types, including strings. These operations could be regarding comparison of strings, combination, removal, or addition of characters/strings. Let's work with them in detail in the following sections.

String Operations in JavaScript

First string operation to discuss is concatenation. This operation allows us to combine multiple strings together.

JAVASCRIPT
1var book = "The Great Gatsby"
2var author = "F. Scott Fitzgerald"
3
4var sentence = book + "was written by " + author
5
6console.log(sentence) //prints "The Great Gatsby was written by F. Scott Fitzgerald"

Other string operations in JavaScript are defined through string properties. They modify the strings according to the specified operation. Let's see some examples.

  • length property of string gives the length of the string.
  • indexOf property gives the index of the first occurence of the character or a substring in the string.
  • toLowerCase() converts the string to lowercase.
JAVASCRIPT
1var book = "The Great Gatsby"
2var author = "F. Scott Fitzgerald"
3
4console.log(book.length) //prints 16
5console.log(author.indexOf("z")) //prints 12
6console.log(author.indexOf("Scott")) //prints 3
7console.log(author.toLowerCase()) //prints "f. scott fitzgerald"

A comprehensive list of string properties is provided here.

Let's test your knowledge. Click the correct answer from the options.

The output to the following code block will be,

JAVASCRIPT
1var name = "Sara"
2var age = "12"
3console.log(name+age)

Click the option that best answers the question.

  • Sara12
  • Sara 12
  • sara 12

Build your intuition. Fill in the missing part by typing it in.

The output of the following code will be ___.

JAVASCRIPT
1var animal = "Giraffe";
2console.log(animal.indexOf("f"));

Write the missing line below.

While working with strings, there are some important properties that need to be taken care of,

  • Strings are immutable. This means that they cannot be reassigned characters. For example, in the example below, we will get an error of item assignment as reassignment of characters is not allowed.
JAVASCRIPT
1var name = "Lee";
2name[0] = "T";
3console.log(name); // 'Lee'
  • To include single quotation inside a string, we need to use double quotation marks to declare them. We cannot use single quotation marks to declare the string if we want to use single quotation marks inside the string.

Summary

In this lesson, we discussed about strings and various string operations that can be performed on them. String operations, as we've seen, are helpful in modifying strings at any time during the program rather than creating the entire string again and again. It also tells us about important properties of the already declared string variables.

One Pager Cheat Sheet

  • In this lesson, we will explore basic types of programming languages, focusing on the string type and operations defined on strings.
  • Strings in programming languages are a data type which is a sequence of characters that can be assigned to variables, accessed and manipulated using various properties and methods in order to perform various operations.
  • Strings in JavaScript can be declared using either single or double quotation marks, as demonstrated in the following code block.
  • Indexing is an important concept when manipulating strings, as they are indexed starting from 0 and not 1.
  • Yes, the code block is valid and declares a string with a value of "India".
  • Strings in most programming languages can contain both lowercase and uppercase letters, and case is important in the output of the code.
  • String operations are defined on different data types, including strings, to perform comparison, combination, removal, and addition of characters/strings.
  • In JavaScript, strings can be manipulated through concatenation, properties such as length, indexOf and toLowerCase(), as outlined in the documentation provided by the Mozilla Developer Network.
  • The code block combines two strings using string concatenation, resulting in Sara12 as the output.
  • The code will output the index position of the letter f in the animal variable, which is 4.
  • Strings are immutable and must be declared with double quotation marks to include single quotation marks inside.
  • We learned about string operations and their benefits, which can help us modify strings without having to recreate them.