Work with Strings

Learning Objectives

Section 9.1: Introduction

What is a String?

A string is a sequence of characters representing text.

let greeting = "Hello, World!";
let name = 'Alice';
let message = `JavaScript is cool`;

console.log(greeting);

String Types

let single = 'text';
let double = "text";
let template = `text with ${variable}`;

Section 9.2: Strings as Set of Characters

String Length

let word = "JavaScript";

console.log(word.length);  // 10

Accessing Characters

let word = "hello";

console.log(word[0]);   // h
console.log(word[4]);   // o
console.log(word.charAt(2));  // l

Iterating Through Strings

let word = "hello";

for (let i = 0; i < word.length; i++) {
    console.log(word[i]);
}

// Or with for...of
for (let char of word) {
    console.log(char);
}

Section 9.3: Manipulating Strings

Case Conversion

let word = "JavaScript";

console.log(word.toUpperCase());   // JAVASCRIPT
console.log(word.toLowerCase());   // javascript

Trimming

let text = "  hello world  ";

console.log(text.trim());   // "hello world"
console.log(text.trimStart());  // "hello world  "
console.log(text.trimEnd());    // "  hello world"

Concatenation

let first = "Hello";
let second = "World";

console.log(first + " " + second);  // Hello World
console.log(first.concat(" ", second));  // Hello World

Template Literals

let name = "Alice";
let age = 25;

let message = `${name} is ${age} years old`;
console.log(message);  // Alice is 25 years old

Slicing and Substring

let word = "JavaScript";

console.log(word.slice(0, 4));     // Java
console.log(word.substring(4, 6)); // Sc
console.log(word.slice(-4));       // cript

Replacing Text

let text = "Hello World";

console.log(text.replace("World", "JavaScript"));  // Hello JavaScript
console.log(text.replaceAll("l", "x"));  // Hexxo Worxd

Section 9.4: Searching Inside a String

indexOf() and lastIndexOf()

let text = "hello world";

console.log(text.indexOf("o"));      // 4
console.log(text.lastIndexOf("o"));  // 7
console.log(text.indexOf("x"));      // -1 (not found)

includes()

let text = "JavaScript is awesome";

console.log(text.includes("Script"));  // true
console.log(text.includes("Python"));  // false

startsWith() and endsWith()

let email = "user@example.com";

console.log(email.startsWith("user"));      // true
console.log(email.endsWith(".com"));        // true
console.log(email.endsWith(".org"));        // false

split() - String to Array

let sentence = "The quick brown fox";

let words = sentence.split(" ");
console.log(words);  // ["The", "quick", "brown", "fox"]

let chars = "hello".split("");
console.log(chars);  // ["h", "e", "l", "l", "o"]

Coding Challenges

Challenge 9.1: Word Information

Task: Analyze a word:

Challenge 9.2: Vowel Count

Task: Count the number of vowels in a sentence

Challenge 9.3: Reverse a Word

Task: Take a word and display it backwards

Example:

Input: "hello"
Output: "olleh"

Challenge 9.4: Palindrome Finder

Task: Check if a word/phrase is a palindrome

Key Takeaways

✅ Strings are sequences of characters
✅ Access using indexing and methods
✅ Use toUpperCase() and toLowerCase()
✅ Split strings with split()
✅ Search with indexOf(), includes(), startsWith()
✅ Template literals allow variable interpolation

Quiz Questions


Next Module: Module 10 - Understand Object Oriented Programming