JavaScript Strings
Strings are sequences of characters enclosed in single or double quotes. They are used to represent text in JavaScript.
One of the most important aspects of JavaScript is its ability to manipulate strings, which are sequences of characters. In this Tutorial, we will explore the basics of JavaScript strings and the various string methods that can be used to manipulate them.
Creating Strings
In JavaScript, strings are created by enclosing a sequence of characters in single or double quotes. Here are some examples of string literals:
let str1 = "Hello, World!";
let str2 = "JavaScript is awesome!";You can also create strings using the String constructor:
let str3 = new String("Hello, World!");However, it is more common to create strings using string literals as shown in the first examples.
String Methods
JavaScript provides a number of built-in methods that can be used to manipulate strings. Here are some common string methods:
length
The length property returns the length of a string:
let str = "Hello, World!";
console.log(str.length); // Output: 13toUpperCase
The toUpperCase method converts a string to uppercase:
let str = "Hello, World!";
console.log(str.toUpperCase()); // Output: HELLO, WORLD!toLowerCase
The toLowerCase method converts a string to lowercase:
let str = "Hello, World!";
console.log(str.toLowerCase()); // Output: hello, world!charAt
The charAt method returns the character at a specified index in a string:
let str = "Hello, World!";
console.log(str.charAt(7)); // Output: WindexOf
The indexOf method returns the index of the first occurrence of a specified substring in a string:
let str = "Hello, World!";
console.log(str.indexOf("World")); // Output: 7concat
The concat method concatenates two or more strings:
let str1 = "Hello, ";
let str2 = "World!";
console.log(str1.concat(str2)); // Output: Hello, World!split
The split method splits a string into an array of substrings based on a specified delimiter:
let str = "Hello, World!";
console.log(str.split(" ")); // Output: ["Hello,", "World!"]slice
The slice method extracts a section of a string and returns it as a new string:
let str = "Hello, World!";
console.log(str.slice(7)); // Output: World!replace
The replace method replaces a specified substring with another substring in a string:
let str = "Hello, World!";
console.log(str.replace("World", "JavaScript")); // Output: Hello, JavaScript!trim
The trim method removes whitespace from both ends of a string:
let str = " Hello, World! ";
console.log(str.trim()); // Output: Hello, World!startsWith
The startsWith method checks if a string starts with a specified substring:
let str = "Hello, World!";
console.log(str.startsWith("Hello")); // Output: trueendsWith
The endsWith method checks if a string ends with a specified substring:
let str = "Hello, World!";
console.log(str.endsWith("JavaScript")); // Output: falseincludes
The includes method checks if a string contains a specified substring:
let str = "Hello, World!";
console.log(str.includes("World")); // Output: truerepeat
The repeat method returns a new string with a specified number of copies of the original string:
let str = "Hello, World!";
console.log(str.repeat(3)); // Output: Hello, World!Hello, World!Hello, World!These are just a few examples of the many string methods available in JavaScript. You can find more information about string methods in the MDN Web Docs.
Conclusion
- Strings are sequences of characters enclosed in single or double quotes.
- JavaScript provides a number of built-in methods that can be used to manipulate strings.
- Some common string methods include
length,toUpperCase,toLowerCase,charAt,indexOf,concat,split,slice,replace,trim,startsWith,endsWith,includes, andrepeat.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
Functions
Functions are reusable blocks of code that perform a specific task. They allow you to break down your code into smaller, more manageable pieces.
JavaScripit Arrays
Arrays are used to store multiple values in a single variable. They are a special type of object that can hold multiple values at once.