Strings

Lesson 7
Author : Afrixi
Last Updated : March, 2023
Javascript - Program the Web
This course covers the basics of programming in Javascript.

Lesson 1: Introduction to Strings In this lesson, we will learn about what strings are and how they are used in JavaScript. A string is a sequence of characters that can be letters, numbers, symbols, or spaces. Strings are used to represent text in JavaScript and are enclosed in either single or double quotes.

Example:

let greeting = "Hello, world!";

In this example, the string "Hello, world!" is assigned to the variable greeting.

Lesson 2: String Methods

In this lesson, we will learn about string methods that can be used to manipulate and work with strings in JavaScript. Here are some common string methods:

length() - returns the length of a string.

toUpperCase() - converts a string to uppercase.

toLowerCase() - converts a string to lowercase.

indexOf() - returns the index of the first occurrence of a specified string.

slice() - extracts a section of a string and returns it as a new string.

Example:

let message = "JavaScript is awesome!";
let length = message.length;
let upperCaseMessage = message.toUpperCase();
let lowerCaseMessage = message.toLowerCase();
let index = message.indexOf("is");
let slicedMessage = message.slice(0, 10);

In this example, we use various string methods to manipulate the string "JavaScript is awesome!".

Lesson 3: String Concatenation

In this lesson, we will learn about string concatenation, which is the process of combining two or more strings into one. In JavaScript, we can use the + operator to concatenate strings.

Example:

let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;

In this example, we concatenate the strings “John” and “Doe” to create the string “John Doe”.

Lesson 4: String Interpolation

In this lesson, we will learn about string interpolation, which is a way to embed expressions into strings. In JavaScript, we can use template literals to interpolate strings.

Example:

let name = "John";
let age = 30;
let message = `My name is ${name} and I am ${age} years old.`;

In this example, we use template literals to interpolate the variables name and age into the string “My name is John and I am 30 years old.”

Lesson 5: String Escaping

In this lesson, we will learn about string escaping, which is the process of adding a backslash () before special characters to prevent them from being interpreted as part of the string. Special characters include quotes, backslashes, and line breaks.

Example:

let message = "He said, \"I'm going to the store.\"";

In this example, we use string escaping to include quotes in the string “He said, “I’m going to the store.””.

Congratulations, you have completed the JavaScript Strings Course for Beginners! With the knowledge gained from this course, you should now be able to work with strings in JavaScript and perform various string manipulations.