JavaScript padStart() Method
JavaScript padStart() method pad a string with another string until it reaches the given length. The padding is applied from the left end of the string.
Example: This example shows the implementation of the above-explained approach.
// Define a string variable
let stone = "Soul";
// Use the padStart() method to add padding characters "Mind "
//to the beginning of the string 'stone'
stone = stone.padStart(9, "Mind ");
// Output the resulting string after padding
console.log(stone);
Output
Mind Soul
Explanation:
The code initializes a string variable stone with the value “Soul”. It then uses the padStart() method to pad the string with spaces or a specified string (“Mind “) until the resulting string reaches a length of 9 characters. Finally, it prints the modified string to the console, resulting in “Mind Soul”.
JavaScript String Methods
JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing.
JavaScript provides a bunch of string methods for representing and manipulating strings. From retrieving specific characters with charAt(), converting strings to uppercase with toUpperCase(), to combining strings with concat(), these methods simplify a wide range of tasks for developers. In this article, we will explore some essential JavaScript string methods that every developer should be familiar with.