JavaScript String Methods – Detailed
Notes
1️⃣ String length
🔹 Definition:
Returns the total number of characters in a string (including spaces).
🔹 Syntax:
[Link]
🔹 Example:
let name = "Ram Kumar";
[Link]([Link]);
🔹 Output:
9
(Spaces are also counted)
2️⃣ String charAt()
🔹 Definition:
Returns the character at a specified index position.
🔹 Syntax:
[Link](index)
🔹 Example:
let name = "Ram";
[Link]([Link](0));
🔹 Output:
R
🔹 Important:
● Index starts from 0
● If index not found → returns empty string
3️⃣ String concat()
🔹 Definition:
Joins two or more strings together.
🔹 Syntax:
[Link](string2)
🔹 Example:
let first = "Ram";
let last = "Kumar";
[Link]([Link](" ", last));
🔹 Output:
Ram Kumar
4️⃣ String substring()
🔹 Definition:
Extracts part of a string between two index positions.
🔹 Syntax:
[Link](start, end)
● start → starting index
● end → ending index (not included)
🔹 Example:
let email = "ram@[Link]";
[Link]([Link](0, 3));
🔹 Output:
ram
5️⃣ String toUpperCase()
🔹 Definition:
Converts entire string to uppercase.
🔹 Syntax:
[Link]()
🔹 Example:
let city = "chennai";
[Link]([Link]());
🔹 Output:
CHENNAI
6️⃣ String toLowerCase()
🔹 Definition:
Converts entire string to lowercase.
🔹 Syntax:
[Link]()
🔹 Example:
let email = "RAM@[Link]";
[Link]([Link]());
🔹 Output:
ram@[Link]
7️⃣ String trim()
🔹 Definition:
Removes extra spaces from beginning and end of string.
🔹 Syntax:
[Link]()
🔹 Example:
let name = " Ram ";
[Link]([Link]());
🔹 Output:
Ram
🔹 Important:
❌ Does NOT remove spaces in middle
✔️ Only start and end
8️⃣ String split()
🔹 Definition:
Splits a string into an array based on a separator.
🔹 Syntax:
[Link]("separator")
🔹 Example:
let sentence = "React is powerful";
[Link]([Link](" "));
🔹 Output:
["React", "is", "powerful"]
9️⃣ Array join()
🔹 Definition:
Joins array elements into a single string.
🔹 Syntax:
[Link]("separator")
🔹 Example:
let words = ["React", "is", "powerful"];
[Link]([Link](" "));
🔹 Output:
React is powerful
📌 Quick Comparison Table
Method Return Purpose
Type
length Number Count characters
charAt() String Get character
concat() String Join strings
substring() String Extract part
toUpperCa String Convert to caps
se()
toLowerCa String Convert to small
se()
trim() String Remove outer
spaces
split() Array Convert string →
array
join() String Convert array →
string
String questions
1️⃣ Student Registration Form Validation
You are building a student registration system.
A student enters their full name with extra spaces.
Convert the name to uppercase and remove extra spaces.
Find the total length of the cleaned name.
Display the first character of the name.
(Use: trim(), toUpperCase(), length, charAt())
2️⃣ Email Username Extractor
A user enters their email: "ramprasad@[Link]".
Extract only the username (before @).
Convert the username to lowercase.
Display the length of the username.
(Use: substring(), toLowerCase(), length)
3️⃣ Product Code Formatter
A product code is entered as " ab123cd ".
Remove extra spaces.
Convert it to uppercase.
Join it with "SKU-" in front of it.
Display the final formatted code.
(Use: trim(), toUpperCase(), concat())
4️⃣ Sentence Word Counter
A user enters a sentence: "React is very powerful library".
Split the sentence into words.
Find how many words are there.
Display the first word in uppercase.
(Use: split(), length, toUpperCase())
5️⃣ File Name Processor
A file name is given as "report_final_version.pdf".
Split the file name using "_".
Join the words using space " ".
Convert the result to lowercase.
Display the formatted file name.
(Use: split(), join(), toLowerCase())
6️⃣ Password Hint Generator
User enters a password " Welcome123 ".
Remove spaces.
Extract the first 4 characters.
Convert them to lowercase.
Display the hint.
(Use: trim(), substring(), toLowerCase())