A String in JavaScript is a sequence of characters used to store text.
let course = "JavaScript";
1. String length
Purpose
Returns the total number of characters in a string.
Syntax
string.length
Example
let company = "OpenAI";
console.log(company.length);
Output
6
Real-Time Example
Checking password length before registration.
2. String charAt()
Purpose
Returns the character at a specified index.
Syntax
string.charAt(index)
Example
let city = "Madurai";
console.log(city.charAt(3));
Output
u
Internal Logic
M a d u r a i
0 1 2 3 4 5 6
Index 3 contains "u".
3. String charCodeAt()
Purpose
Returns the Unicode value (UTF-16 code) of a character.
Example
let letter = "A";
console.log(letter.charCodeAt(0));
Output
65
More Examples
console.log("a".charCodeAt(0));
Output:
97
4. String codePointAt()
Purpose
Returns the Unicode code point of a character.
Useful for emojis and special symbols.
Example
let emoji = "😊";
console.log(emoji.codePointAt(0));
Output
128522
Difference
console.log("😊".charCodeAt(0));
console.log("😊".codePointAt(0));
codePointAt() gives the actual Unicode value.
5. String concat()
Purpose
Combines two or more strings.
Example
let firstName = "Annapoorani";
let lastName = " Kadhiravan";
let fullName = firstName.concat(lastName);
console.log(fullName);
Output
Annapoorani Kadhiravan
Alternative
console.log(firstName + lastName);
6. String at()
Purpose
Returns character at a specific position.
Supports negative indexing.
Example
let language = "JavaScript";
console.log(language.at(0));
console.log(language.at(-1));
Output
J
t
7. String [ ]
Purpose
Access characters using bracket notation.
Example
let laptop = "Dell";
console.log(laptop[0]);
console.log(laptop[2]);
Output
D
l
Difference
console.log(laptop.charAt(0));
console.log(laptop[0]);
Both return same result.
8. String slice()
Purpose
Extracts part of a string.
Syntax
string.slice(start,end)
Example
let course = "JavaScript";
console.log(course.slice(0,4));
Output
Java
Negative Index
console.log(course.slice(-6));
Output
Script
9. String substring()
Purpose
Extracts characters between indexes.
Example
let company = "Microsoft";
console.log(company.substring(0,5));
Output
Micro
Difference from slice()
let str = "JavaScript";
console.log(str.slice(-6));
console.log(str.substring(-6));
Output:
Script
JavaScript
substring() doesn't support negative indexes.
10. String substr()
⚠️ Deprecated (Avoid in new projects)
Purpose
Extracts characters based on start position and length.
Example
let city = "Chennai";
console.log(city.substr(2,4));
Output
enna
Explanation
Start at index 2
Take 4 characters
11. String toUpperCase()
Purpose
Converts string to uppercase.
Example
let name = "annapoorani";
console.log(name.toUpperCase());
Output
ANNAPOORANI
12. String toLowerCase()
Purpose
Converts string to lowercase.
Example
let company = "OPENAI";
console.log(company.toLowerCase());
Output
openai
13. String isWellFormed()
Purpose
Checks whether a string contains valid Unicode characters.
Example
let text = "Hello";
console.log(text.isWellFormed());
Output
true
Use Case
Unicode validation before processing text.
14. String toWellFormed()
Purpose
Converts malformed Unicode into valid Unicode.
Example
let text = "\uD800";
console.log(text.toWellFormed());
Output
�
Use Case
Cleaning corrupted text data.
15. String trim()
Purpose
Removes spaces from beginning and end.
Example
let email = " user@gmail.com ";
console.log(email.trim());
Output
user@gmail.com
16. String trimStart()
Purpose
Removes spaces only from beginning.
Example
let text = " Hello";
console.log(text.trimStart());
Output
Hello
17. String trimEnd()
Purpose
Removes spaces only from end.
Example
let text = "Hello ";
console.log(text.trimEnd());
Output
Hello
18. String padStart()
Purpose
Adds characters at the beginning until desired length.
Example
let orderId = "123";
console.log(orderId.padStart(6,"0"));
Output
000123
Real-Time Example
Generating invoice numbers.
19. String padEnd()
Purpose
Adds characters at the end.
Example
let code = "JS";
console.log(code.padEnd(5,"*"));
Output
JS***
20. String repeat()
Purpose
Repeats a string multiple times.
Example
let star = "*";
console.log(star.repeat(5));
Output
*****
Real-Time Example
Printing separators.
21. String replace()
Purpose
Replaces first matching occurrence.
Example
let sentence = "I love Java. Java is powerful.";
console.log(sentence.replace("Java","JavaScript"));
Output
I love JavaScript. Java is powerful.
Note
Only first occurrence is replaced.
22. String replaceAll()
Purpose
Replaces all matching occurrences.
Example
let sentence = "Java Java Java";
console.log(sentence.replaceAll("Java","JS"));
Output
JS JS JS
23. String split()
Purpose
Converts string into array.
Example
let skills = "HTML,CSS,JavaScript";
let result = skills.split(",");
console.log(result);
Output
["HTML", "CSS", "JavaScript"]
Split by Space
let sentence = "Learning JavaScript Daily";
console.log(sentence.split(" "));
Output
["Learning","JavaScript","Daily"]
Quick Comparison Table
| Method | Purpose |
|---|---|
| length | Count characters |
| charAt() | Get character |
| charCodeAt() | Unicode value |
| codePointAt() | Unicode code point |
| concat() | Join strings |
| at() | Access character (supports negative index) |
| [ ] | Access character |
| slice() | Extract part of string |
| substring() | Extract text (no negative index) |
| substr() | Extract by length (deprecated) |
| toUpperCase() | Convert to uppercase |
| toLowerCase() | Convert to lowercase |
| isWellFormed() | Check valid Unicode |
| toWellFormed() | Fix invalid Unicode |
| trim() | Remove spaces both sides |
| trimStart() | Remove left spaces |
| trimEnd() | Remove right spaces |
| padStart() | Add characters at beginning |
| padEnd() | Add characters at end |
| repeat() | Repeat string |
| replace() | Replace first match |
| replaceAll() | Replace all matches |
| split() | Convert string to array |
References:
https://www.w3schools.com/js/js_string_methods.asp
Top comments (0)