Home 〉 Javascript Tutorials 〉 JavaScript String & String Methods Explained with Examples
JavaScript String & String Methods Explained with Examples !
Want to understand how JavaScript works with text data using strings and their powerful methods? Strings are one of the most important data types in JavaScript and mastering them is essential for every developer. This post covers everything from basic string syntax to all major JavaScript string methods like charAt(), concat(), includes(), replace(), slice(), split(), toUpperCase(), and many more. Each method is explained with simple examples that are easy to follow. We’ll also look at some related topics like how strings are immutable in JavaScript. Check out the complete blog post below to become confident with JavaScript strings and their usage.
In JavaScript, a string is a sequence of characters used to represent text. Strings can be written using single quotes (' '), double quotes (" "), or backticks (` ` for template literals). Strings are one of the most commonly used data types and JavaScript provides many built-in methods to manipulate them.
html syntax ✍
let str1 = "Hello World";
let str2 = 'JavaScript String';
let str3 = `Template String`;
You can use any of the above methods to create strings. Template strings (using backticks) are especially useful when including variables or expressions inside a string using ${}.
Returns the character at the specified index.
Example 📄
let str = "JavaScript";
alert(str.charAt(0)); // Output: J
Explanation: Indexing starts from 0. So, charAt(0) returns the first character.
Joins two or more strings.
Example 📄
let first = "Hello";
let second = "World";
let result = first.concat(" ", second);
console.log(result); // Output: Hello World
Checks if a string contains a specified value.
Example 📄
let text = "Learn JavaScript";
console.log(text.includes("Java")); // true
Returns the index of the first occurrence of a value. Returns -1 if not found.
Example 📄
let str = "Programming";
console.log(str.indexOf("g")); // 3
Replaces a value with another value in a string.
Example 📄
let sentence = "I love Java";
let newSentence = sentence.replace("Java", "JavaScript");
console.log(newSentence); // I love JavaScript
Extracts a part of the string and returns it.
Example 📄
let msg = "JavaScript";
console.log(msg.slice(4, 10)); // Script
Splits a string into an array using a separator.
Example 📄
let fruits = "Apple,Banana,Cherry";
console.log(fruits.split(",")); // ["Apple", "Banana", "Cherry"]
Converts the string to lowercase or uppercase.
Example 📄
let greeting = "HeLLo";
console.log(greeting.toLowerCase()); // hello
console.log(greeting.toUpperCase()); // HELLO
Removes whitespace from both ends of the string.
Example 📄
let name = " John Doe ";
console.log(name.trim()); // "John Doe"
Returns the number of characters in a string.
Example 📄
let word = "JavaScript";
console.log(word.length); // 10
Strings in JavaScript are immutable. This means you cannot change characters inside the string directly. Instead, you create a new string whenever you apply any operation like replace() or slice(). Read more about string data type on Wikipedia.
Understanding and using JavaScript string methods effectively can save you time and make your code cleaner. From basic methods like charAt() to powerful ones like replace(), split(), and slice(), each function serves a unique purpose. Make sure to practice with real examples and use these methods in your projects. Also, explore more in our complete JavaScript tutorial for beginners to deepen your knowledge.
A JavaScript string is a sequence of characters used to represent text data. It can be written using single, double, or backtick quotes.
No, strings in JavaScript are immutable. Any method applied returns a new string instead of modifying the original one.
You can use the split() method with a space as a separator like string.split(" ").
Use the includes() method. For example, "Hello World".includes("World") will return true.
Both return a portion of the string, but slice() can accept negative indices, whereas substring() does not.