JavaScript Tutorial in Hindi #16 | String Methods (2025)
1. .length
Returns the number of characters in a string.
js
CopyEdit
let name = "Mohit";
console.log(name.length); // 5
2. .toUpperCase()
/ .toLowerCase()
Convert string to uppercase or lowercase.
js
CopyEdit
let name = "Mohit";
console.log(name.toUpperCase()); // "MOHIT"
console.log(name.toLowerCase()); // "mohit"
3. .charAt(index)
Returns the character at the given index.
js
CopyEdit
let name = "Mohit";
console.log(name.charAt(0)); // "M"
4. .indexOf("text")
Returns the first index of the given character/word. Returns -1
if not found.
js
CopyEdit
let text = "Hello World";
console.log(text.indexOf("o")); // 4
console.log(text.indexOf("z")); // -1
5. .includes("text")
Checks if a string contains the given substring. Returns true
or false
.
js
CopyEdit
let text = "Learn JavaScript";
console.log(text.includes("Java")); // true
6. .slice(start, end)
vs .substring(start, end)
Both return a part of the string.
✅ Difference:
Featureslice()substring() | ||
Accepts negative index | ✅ Yes | ❌ No |
Usage | slice(1, 4) → "ear" | substring(1, 4) → "ear" |
js
CopyEdit
let str = "Learning";
console.log(str.slice(1, 4)); // "ear"
console.log(str.substring(1, 4)); // "ear"
console.log(str.slice(-3)); // "ing"
7. .replace("old", "new")
Replaces the first match of a string or pattern.
js
CopyEdit
let text = "I love JavaScript";
console.log(text.replace("love", "like")); // "I like JavaScript"
8. .split("separator")
Splits a string into an array.
js
CopyEdit
let names = "Mohit,John,Alice";
console.log(names.split(",")); // ["Mohit", "John", "Alice"]
9. .trim()
Removes extra spaces from start and end of the string.
js
CopyEdit
let msg = " Hello World ";
console.log(msg.trim()); // "Hello World"
10. .concat()
(Optional)
Joins multiple strings (similar to +
operator).
js
CopyEdit
let a = "Hello";
let b = "World";
console.log(a.concat(" ", b)); // "Hello World"