Day-5: JavaScript String

For Inspect the code ( Ctrl + Shift + I )

A String is simply a collection of characters (letters, numbers, spaces, or symbols) wrapped inside quotation marks. In JavaScript, we can use single quotes ('...'), double quotes ("..."), or backticks (`...`) to create a string.

The Master String Grouping

A. Building Strings:

(1) Concatenation:

Concatenation means joining two or more strings together to form a single string. Traditionally, this is done using the plus sign (+) operator.

(2) Template Literals:

Template Literals are a modern, cleaner way to build strings using backticks (`...`) instead of quotes. Instead of using the + operator, We can inject variables directly into the string using the ${variable} syntax.

B. Inspecting Strings

(3) The .length Property

The .length property counts the total number of characters inside a string, including letters, numbers, symbols, and spaces. It is a property, not a method, so it does not use parentheses ().

(4) Character Access (.charAt() & Bracket Notation)

Character access allows you to grab a single character from a string based on its position, known as its index. In JavaScript, tracking always starts at index 0 (the first character)..

C. Cleaning & Modifying Strings

(5) The .trim() Method

The .trim() method removes whitespace from both ends of a string.

(6) Case Conversions (.toUpperCase() & .toLowerCase())

The .toUpperCase() method converts a string to uppercase, while the .toLowerCase() method converts it to lowercase.

D. Searching & Cutting Strings

(7) Searching Strings (.includes() & .indexOf())

These tools help you search a string to see if a specific letter or word exists inside it.

♦ .includes() answers with a simple True or False.

♦ Use .includes() when you just want a clean true or false answer to check if a word exists.

♦ .indexOf() returns the exact starting position number (index) where the word begins. If it cannot find the word, it returns -1.

♦ Use .indexOf() when you need the exact position number of a word.

(8) Extracting Substrings (.slice())

The .slice(start, end) method cuts out a piece of a string and returns it as a new string..

(9) Replacing Text (.replace() & .replaceAll())

The .replace() method finds a specific word or character inside a string and swaps it out for a new value. Standard .replace() only changes the very first match it finds, while .replaceAll() updates every single match across the entire string..

E. Data Conversions (String ⇄ Array)

(10) Splitting Strings (.split())

The .split() method splits a string into an array of substrings based on a specified separator.

♦ .split() moves data from a String ➡️ Array (Cuts it up).

(11) Joining Strings (.join())

The .join() method joins all elements of an array into a string, with an optional separator.

♦ .join() moves data from an Array ➡️ String (Glues it together).