# JavaScript String Methods

> Source: <https://dev.to/annapoo/javascript-string-methods-3kll>
> Published: 2026-06-26 03:01:39+00:00

A **String** in JavaScript is a sequence of characters used to store text.

``` js
let course = "JavaScript";
```

Returns the total number of characters in a string.

```
string.length
js
let company = "OpenAI";

console.log(company.length);
6
```

Checking password length before registration.

Returns the character at a specified index.

```
string.charAt(index)
js
let city = "Madurai";

console.log(city.charAt(3));
u
M a d u r a i
0 1 2 3 4 5 6
```

Index 3 contains "u".

Returns the Unicode value (UTF-16 code) of a character.

``` js
let letter = "A";

console.log(letter.charCodeAt(0));
65
console.log("a".charCodeAt(0));
```

Output:

```
97
```

Returns the Unicode code point of a character.

Useful for emojis and special symbols.

``` js
let emoji = "😊";

console.log(emoji.codePointAt(0));
128522
console.log("😊".charCodeAt(0));
console.log("😊".codePointAt(0));
```

`codePointAt()`

gives the actual Unicode value.

Combines two or more strings.

``` js
let firstName = "Annapoorani";
let lastName = " Kadhiravan";

let fullName = firstName.concat(lastName);

console.log(fullName);
Annapoorani Kadhiravan
console.log(firstName + lastName);
```

Returns character at a specific position.

Supports negative indexing.

``` js
let language = "JavaScript";

console.log(language.at(0));
console.log(language.at(-1));
J
t
```

Access characters using bracket notation.

``` js
let laptop = "Dell";

console.log(laptop[0]);
console.log(laptop[2]);
D
l
console.log(laptop.charAt(0));
console.log(laptop[0]);
```

Both return same result.

Extracts part of a string.

```
string.slice(start,end)
js
let course = "JavaScript";

console.log(course.slice(0,4));
Java
console.log(course.slice(-6));
Script
```

Extracts characters between indexes.

``` js
let company = "Microsoft";

console.log(company.substring(0,5));
Micro
js
let str = "JavaScript";

console.log(str.slice(-6));
console.log(str.substring(-6));
```

Output:

```
Script
JavaScript
```

`substring()`

doesn't support negative indexes.

⚠️ Deprecated (Avoid in new projects)

Extracts characters based on start position and length.

``` js
let city = "Chennai";

console.log(city.substr(2,4));
enna
Start at index 2
Take 4 characters
```

Converts string to uppercase.

``` js
let name = "annapoorani";

console.log(name.toUpperCase());
ANNAPOORANI
```

Converts string to lowercase.

``` js
let company = "OPENAI";

console.log(company.toLowerCase());
openai
```

Checks whether a string contains valid Unicode characters.

``` js
let text = "Hello";

console.log(text.isWellFormed());
true
```

Unicode validation before processing text.

Converts malformed Unicode into valid Unicode.

``` js
let text = "\uD800";

console.log(text.toWellFormed());
�
```

Cleaning corrupted text data.

Removes spaces from beginning and end.

``` js
let email = "  user@gmail.com  ";

console.log(email.trim());
user@gmail.com
```

Removes spaces only from beginning.

``` js
let text = "   Hello";

console.log(text.trimStart());
Hello
```

Removes spaces only from end.

``` js
let text = "Hello   ";

console.log(text.trimEnd());
Hello
```

Adds characters at the beginning until desired length.

``` js
let orderId = "123";

console.log(orderId.padStart(6,"0"));
000123
```

Generating invoice numbers.

Adds characters at the end.

``` js
let code = "JS";

console.log(code.padEnd(5,"*"));
JS***
```

Repeats a string multiple times.

``` js
let star = "*";

console.log(star.repeat(5));
*****
```

Printing separators.

Replaces first matching occurrence.

``` js
let sentence = "I love Java. Java is powerful.";

console.log(sentence.replace("Java","JavaScript"));
I love JavaScript. Java is powerful.
```

Only first occurrence is replaced.

Replaces all matching occurrences.

``` js
let sentence = "Java Java Java";

console.log(sentence.replaceAll("Java","JS"));
JS JS JS
```

Converts string into array.

``` js
let skills = "HTML,CSS,JavaScript";

let result = skills.split(",");

console.log(result);
["HTML", "CSS", "JavaScript"]
js
let sentence = "Learning JavaScript Daily";

console.log(sentence.split(" "));
["Learning","JavaScript","Daily"]
```

| 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](https://www.w3schools.com/js/js_string_methods.asp)
