In JavaScript programming, a string is an important and common data type. A string represents a sequence of characters, such as a word, a sentence, or a paragraph.
To declare a string in JavaScript, we can use single quotes ('') or double quotes ("").
let myString1 = 'Đây là một chuỗi sử dụng dấu nháy đơn.';
let myString2 = "Đây là một chuỗi sử dụng dấu nháy kép.";
We can also use backticks (`) to create template strings, allowing us to insert JavaScript expressions into the string.
let name = "John";
let age = 30;
let myString3 = `Xin chào, tôi là ${name} và tôi ${age} tuổi.`;
In addition, JavaScript also supports other operators and methods to work with strings.
Operator +: When using the + operator, we can concatenate strings together.
let string1 = "Hello";
let string2 = "World";
let result = string1 + " " + string2;
console.log(result); // Output: Hello World
The length property returns the number of characters in a string.
let myString = "Hello World";
console.log(myString.length); // Output: 11
The charAt method is used to access the character at a specific position in a string. Meanwhile, charCodeAt returns the Unicode value of the character at the given position.
const myString = "Hello, World!";
console.log(myString.charAt(0)); // Output: H
console.log(myString.charCodeAt(0)); // Output: 72
The substring() method extracts a portion of a string based on a start and end index.
let myString = "Hello World";
console.log(myString.substring(0, 5)); // Output: Hello
The toUpperCase() method converts a string to uppercase, while toLowerCase() converts a string to lowercase.
let myString = "Hello World";
console.log(myString.toUpperCase()); // Output: HELLO WORLD
console.log(myString.toLowerCase()); // Output: hello world
The indexOf() method returns the first occurrence of a substring in the original string.
let myString = "Hello World";
console.log(myString.indexOf("World")); // Output: 6
The split method is used to divide a string into an array of elements based on a separator character. Meanwhile, join is used to combine elements in an array into a string.
const myString = "Hello, World!";
console.log(myString.split(" ")); // Output: ["Hello,", "World!"]
const myArray = ["Hello", "World"];
console.log(myArray.join(", ")); // Output: Hello, World
The replace method is used to replace a part of a string with a new value.
const myString = "Hello, World!";
console.log(myString.replace("World", "Universe"));
// Output: Hello, Universe!
The padStart method is used to add padding characters to the beginning of a string until the desired length is reached.
const myString = "Hello";
console.log(myString.padStart(10, "X")); // Output: "XXXXHello"
The padEnd method is used to add padding characters to the end of a string until it reaches the desired length.
const myString = "Hello";
console.log(myString.padEnd(10, "X")); // Output: "HelloXXXX"
Using Space Padding: Instead of using a specific padding character, we can use whitespace to align the string.
const myString = "Hello";
console.log(myString.padStart(10)); // Output: " Hello"
console.log(myString.padEnd(10)); // Output: "Hello "
Using String Padding in Date Formatting: String padding is very useful when working with date formatting. For example, we can use string padding to display the date and month in the format DD/MM/YYYY.
const day = "7";
const month = "12";
const year = "2023";
const formattedDate = day.padStart(2, "0") + "/" + month.padStart(2, "0") + "/" + year;
console.log(formattedDate); // Output: "07/12/2023"