๐Ÿงต Day 7 โ€” Strings in JavaScript (Methods & Template Literals)

๐Ÿ”ค What is a String?

A string is a sequence of characters used to represent text.

jsCopyEditlet greeting = "Hello, world!";

Strings are immutable โ€” you can create new strings from existing ones, but you can't change a character inside a string directly.


โœ๏ธ Ways to create strings

jsCopyEditlet s1 = "Hello";
let s2 = 'World';
let s3 = `Template literal with ${s1} ${s2}`; // backticks

Template literals (backticks) let you embed expressions with ${...}, and support multi-line strings.


๐Ÿ”ง Useful String Methods (with examples)

  • length โ€” number of characters

      jsCopyEdit"abc".length // 3
    
  • charAt(index) / bracket notation

      jsCopyEditlet str = "JavaScript";
      str.charAt(0)   // "J"
      str[1]          // "a"
    
  • toUpperCase() / toLowerCase()

      jsCopyEdit"hi".toUpperCase() // "HI"
    
  • trim() โ€” remove whitespace ends

      jsCopyEdit"  hello  ".trim() // "hello"
    
  • indexOf(sub) / lastIndexOf(sub)

      jsCopyEdit"banana".indexOf("an") // 1
    
  • includes(sub) โ€” boolean

      jsCopyEdit"hello".includes("ll") // true
    
  • slice(start, end) / substring(start, end)

      jsCopyEdit"JavaScript".slice(4, 10) // "Script"
    
  • split(separator) โ€” convert to array

      jsCopyEdit"a,b,c".split(",") // ["a","b","c"]
    
  • replace(old, new) (first occurrence) and with regex for all

      jsCopyEdit"red blue red".replace("red", "green") // "green blue red"
    
  • repeat(n) โ€” repeat string

      jsCopyEdit"ha".repeat(3) // "hahaha"
    
  • startsWith() / endsWith()

      jsCopyEdit"hello".startsWith("he") // true
    

๐Ÿง  Template Literals (Backticks)

Template literals make string construction clearer and allow interpolation and newlines:

jsCopyEditlet name = "Gift";
let msg = `Hello ${name},
Welcome to Day 7!`;

They also support expressions:

jsCopyEdit`2 + 3 = ${2 + 3}` // "2 + 3 = 5"

๐Ÿงช Day 7 Practice Problems (Easy โ†’ Hard)

1) ๐ŸŸข Easy โ€” Access & Length

Given let city = "Lagos"; print the first character and the string length.

Solution

jsCopyEditlet city = "Lagos";
console.log(city[0]);        // "L"
console.log(city.length);    // 5

2) ๐ŸŸก Intermediate โ€” Capitalize Name

Write capitalize(name) that returns the name with the first letter uppercase and the rest lowercase. Example: "gIfT" โ†’ "Gift".

Solution

jsCopyEditfunction capitalize(name) {
  if (!name) return "";
  return name[0].toUpperCase() + name.slice(1).toLowerCase();
}

console.log(capitalize("gIfT")); // "Gift"

Explanation: slice(1) returns substring from index 1 to end; toLowerCase() normalizes the rest.


3) ๐Ÿ”ด Hard โ€” Reverse Words in a Sentence

Write reverseWords(sentence) that reverses the order of words (not characters).
Input: "I love JavaScript" โ†’ Output: "JavaScript love I"

Solution

jsCopyEditfunction reverseWords(sentence) {
  return sentence
    .trim()
    .split(/\s+/)     // split on one or more spaces
    .reverse()
    .join(" ");
}

console.log(reverseWords("I love JavaScript")); // "JavaScript love I"

Notes: trim() removes extra ends; split(/\s+/) handles multiple spaces robustly.


โœ… Recap

  • Strings are immutable text values.

  • Use template literals for interpolation and multi-line strings.

  • Key methods: slice, split, replace, indexOf, includes, toUpperCase, trim.

  • Practice manipulating strings and combining methods.

1
Subscribe to my newsletter

Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

God'sgift Samuel
God'sgift Samuel

About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.