JavaScript String Methods

JavaScript provides a variety of pre-existing functions for manipulating strings. Strings play a crucial role in various tasks such as collecting user input, formatting text, and handling data. JavaScript offers numerous methods for working with strings, enabling developers to retrieve, modify, and analyze string data effectively. This article will explore different string methods along with practical examples.

What is a String?

In JavaScript, a string is a series of characters enclosed in single ('), double (") or backticks (`) quotes. Essentially, a string is a data type used in JavaScript to manage and modify text within a program. Strings are immutable, meaning their content cannot be directly altered, although there are various methods to interact with them. They can consist of letters (A-Z, a-z), numbers (0-9), special characters (@, #, $, &, %), and white spaces (spaces, tabs, newlines, etc). JavaScript treats strings as objects, offering built-in functions to efficiently extract, locate, substitute, and manipulate text.

String length in JavaScript

The size of a string encompasses spaces and special characters, serving as a valuable tool for validating user input, verifying string length, or making decisions based on string size.

Example

Example

let text = "Hello, JavaScript!";

console.log("Length:", text.length);



let emptyString = "";

console.log("Length:", emptyString.length);



let longString = "This is a very long string!";

console.log("Length:", longString.length);

Output:

Output

Length: 18

Length: 0

Length: 27

Explanation:

In the provided instance, three variables are declared with the names text, emptyString, and longString using the let keyword. Each variable is initialized with string values. The length property of a string is employed to determine the character count in each string, and the outcomes are displayed in the console. The initial string has a length of 18 characters, the second string has a length of 0 characters, and the third string has a length of 27 characters.

String charAt in JavaScript

The charAt function retrieves and returns a specific character located at a particular index within a string. Indexing begins at 0. If the index provided is beyond the range of the string, the function will return an empty string.

Example

Example

let text = "Example";

console.log("Character at index 4:", text.charAt(4));

console.log("Character at index 0:", text.charAt(0));

console.log("Last character:", text.charAt(text.length - 1));

console.log("Character at index 20:", text.charAt(20));

Output:

Output

Character at index 4: n

Character at index 0: T

Last character: h

Character at index 20:

Explanation:

Consider this instance where a variable, denoted as text, is established using the let keyword and is assigned a string value. The method charAt is employed here to fetch the character located at a specific index. For instance, charAt(4) retrieves the character at index 4, which is 'n'. Similarly, charAt(0) retrieves the character at index 0, which is 'T'. Furthermore, charAt(text.length - 1) fetches the final character, which is 'h'. Attempting to extract the character at index 20 using charAt(20) is unsuccessful since the string's length is only 10, resulting in the return of an empty string ("").

String charCodeAt in JavaScript

The method charCodeAt retrieves the Unicode value of the character positioned at a specific index within a string. It proves to be useful when dealing with character encodings or transformations.

Example

Example

let text = "Example";

console.log("ASCII code at index 1:", text.charCodeAt(1));

console.log("ASCII code at index 0:", text.charCodeAt(0));

console.log("ASCII code at last index:", text.charCodeAt(text.length - 1));

Output:

Output

ASCII code at index 1: 112

ASCII code at index 0: 84

ASCII code at last index: 104

Explanation:

In the provided illustration, a variable called text was declared using the let keyword. The purpose of the charCodeAt function in this case is to retrieve the Unicode representation of the character located at a specific position within a string, after which the outcome is displayed in the console. In this context, charCodeAt(1), charCodeAt(0), and charCodeAt(text.length-1) are employed to obtain the Unicode values corresponding to the characters situated at index 1, index 0, and the final index of the string. The results returned are 112, 84, and 104 respectively.

String at in JavaScript

The at function is used to access characters based on their position in the string, and it enables the use of negative indexes to retrieve characters from the string's end. This feature simplifies accessing characters from the end of the string in comparison to standard indexing.

Note: The at method for strings was introduced in ECMAScript 2022 (ES13). It runs perfectly in the ES13 environment, but doesn't work in older JS environments.

Example

Example

let text = "Example";

console.log("Character at index -1:", text.at(-1));

console.log("Character at index -5:", text.at(-5));

console.log("Character at index 3:", text.at(3));

console.log("Character at index 10:", text.at(10));

Output:

Output

Character at index -1: h

Character at index -5: t

Character at index 3: i

Character at index 10: undefined

Explanation:

In this instance, a variable called "content" is declared utilizing the const keyword, and a string value is initialized to it. The charAt function is employed in this scenario to retrieve a character at a designated index, with the outcomes being displayed in the console. In this context, charAt(-1) yields 'd', charAt(-5) yields 'e', charAt(3) yields 'i', and charAt(10) yields 'undefined' due to the string having a length of 10 and indexing commencing from 0, making the final index 9.

String slice in JavaScript

The slice function is utilized for extracting a portion of a string and presenting it as a new string. It requires two parameters: the starting and ending positions. If the ending index is left out, the slice will cover the string until its end.

Example

Example

let text = "JavaScript Programming";

console.log("Slice from 0 to 10:", text.slice(0, 10));

console.log("Slice from 11 to end:", text.slice(11));

console.log("Last 5 characters:", text.slice(-5));

console.log("Slice excluding last 10 characters:", text.slice(0, -10));

Output:

Output

Slice from 0 to 10: JavaScript

Slice from 11 to end: Programming

Last 5 characters: mming

Slice excluding last 10 characters: JavaScript P

Explanation:

In this instance, we have created a variable called "text" using the let keyword and assigned a string value to it. The slice function is then employed to extract specific sections of the string, and the outcomes are displayed in the console.

The method slice(0, 10) retrieves characters starting from the first position up to the 10th position, resulting in 'JavaScript'. When utilizing slice(11), it fetches characters from the 11th position to the end, yielding 'Programming'. Alternatively, slice(-5) captures the last 5 characters, producing 'mming'. Lastly, slice(0, -10) acquires characters from the beginning up to the 10th character from the end, resulting in 'JavaScript P'.

String substring in JavaScript

The method substring retrieves a segment of the string by specifying start and end positions. Unlike slice, this method does not support negative indexes. If the starting index is greater than the ending index, the method will interchange them automatically.

Example

Example

let text = "Example";

console.log("Substring from 0 to 4:", text.substring(0, 4));

console.log("Substring from 4 to 0 (auto swap):", text.substring(4, 0));

console.log("Substring from 4 to end:", text.substring(4));

Output:

Output

Substring from 0 to 4: Tpoi

Substring from 4 to 0 (auto swap): Tpoi

Substring from 4 to end: ntTech

Explanation:

In this instance, we have declared a variable called text using the let keyword and initialized it with a string value. The substring function is employed to retrieve a section of the string, and the outcome is displayed in the console.

The method text.substring(0, 4) retrieves characters starting from index 0 up to, but not including, index 4. This operation would yield 'Tpoi'. In contrast, when using text.substring(4, 0), the arguments are automatically swapped to mimic substring(0, 4) behavior, resulting in 'Tpoi'. Lastly, text.substring(4) fetches characters from index 4 to the end of the string, producing 'ntTech'.

Note: The slice method and substring method are used for the same work but the differences are that in substring, if startIndex is greater than endIndex, then it automatically swaps the two arguments and it treats 0 if startIndex or endIndex is negative or NaN.

When using the slice function, if the starting index is larger than the ending index, the result will be an empty string. Negative indices in slice are treated as positions relative to the end of the string, with -1 representing the last index.

String substr in JavaScript

The substr function retrieves a specified number of characters from a string, starting at a designated position. However, it has been deprecated in favor of the slice method, which serves the same purpose.

Example

Example

let text = "Example";

console.log("Substr from 4, length 6:", text.substr(4, 6));

console.log("Substr from 0, length 4:", text.substr(0, 4));

console.log("Substr from -6, length 6:", text.substr(-6, 6));

Output:

Output

Substr from 4, length 6: ntTech

Substr from 0, length 4: Tpoi

Substr from -6, length 6: ntTech

Explanation:

Within the provided code snippet, a variable called text was established via the let keyword, containing a string value. Through the utilization of the substr function, a segment of the string was isolated and subsequently showcased in the console output. Specifically, the expressions text.substr(4, 6), text.substr(0, 4), and text.substr(-6, 6) yield 'ntTech', 'Tpoi', and 'ntTech' correspondingly.

String toUpperCase in JavaScript

The toUpperCase function is employed to transform all the characters within a string to uppercase. It proves to be beneficial for standardizing text input or for utilization in comparisons that are not case-sensitive.

Example

Example

let text = "hello";

console.log("Uppercase:", text.toUpperCase());



let mixedCase = "C# Tutorial users";

console.log("Uppercase:", mixedCase.toUpperCase());

Output:

Output

Uppercase: HELLO

Uppercase: C# Tutorial USERS

Explanation:

In this instance, we have declared two variables, namely text and mixedCase, using the let keyword and assigned them string values. We made use of the toUpperCase function to convert all characters in the strings to uppercase and then displayed the outcomes on the console. Specifically, text.toUpperCase converts 'hello' to 'HELLO', while mixedCase.toUpperCase changes 'C# Tutorial users' to 'C# Tutorial USERS'.

String toLowerCase in JavaScript

The method toLowerCase is employed to convert a string's value to lowercase, which proves beneficial for conducting case-insensitive searches or comparisons.

Example

Example

let text = "HELLO";

console.log("Lowercase:", text.toLowerCase());



let mixedCase = "JavaScript";

console.log("Lowercase:", mixedCase.toLowerCase());

Output:

Output

Lowercase: hello

Lowercase: javascript

Explanation:

In the previous instance, we declared a duo of variables denoted as text and mixedCase by leveraging the let keyword and allocated string values to them. The toLowerCase function is employed in this scenario to convert each character of the string to lowercase, and the outcomes are displayed in the console. Specifically, text.toLowerCase converts 'HELLO' to 'hello', while mixedCase.toLowerCase converts 'JavaScript' to 'javascript'.

String concat in JavaScript

The concat method is commonly employed to merge multiple strings. While the outcome achieved by concat is identical to that of the + operator, it can enhance code readability especially in intricate string compositions.

Example

Example

let text1 = "Hello";

let text2 = "Example";

let text3 = "Users";

console.log("Concatenated string:", text1.concat(" ", text2));

console.log(

  "Concatenated with multiple strings:",

  text1.concat(" ", text2, " ", text3)

);



let emptyText = "";

console.log("Concatenating empty string:", text1.concat(emptyText));

Output:

Output

Concatenated string: Hello World

Concatenated with multiple strings: Hello Users

Concatenating empty string: Hello

Explanation:

In this instance, we have declared three variables, namely text1, text2, and text3, using the let keyword and initialized them with string values. The concat function is employed to combine the strings from these three variables. Specifically, text1.concat(" ", text2) merges text1 and text2 with a space character resulting in 'Hello World'. Similarly, text1.concat(" ", text2, " ", text3) combines text1, text2, and text3 with spaces leading to 'Hello Users'. Lastly, text1.concat(emptyText) merges text1 with emptyText resulting in 'Hello'.

String trim in JavaScript

The trim function eliminates spaces from the beginning and end of a string, making it useful for sanitizing user input before saving it in a database or performing additional data operations.

Example

Example

let text = "  Hello World  ";

console.log("Trimmed string:", text.trim());



let spacedString = "   JavaScript   ";

console.log("Trimmed string:", spacedString.trim());

Output:

Output

Trimmed string: Hello World

Trimmed string: JavaScript

Explanation:

Within this code snippet, a pair of variables, namely text and spacedString, have been declared using the let keyword with assigned string values. The trim function is employed to eliminate any leading or trailing spaces from the strings, and the outcomes are then displayed on the console.

The method text.trim eliminates additional spaces from a string and yields 'Hello World', while spacedString.trim removes surplus spaces and yields 'JavaScript'.

String trimStart in JavaScript

The trimStart function behaves similarly to eliminating leading spaces from the beginning of a string. This method proves to be beneficial when dealing with scenarios involving indentations or spaces at the start of a string.

Example

Example

let text = "  Hello World";

console.log("Trimmed start:", text.trimStart());



let anotherText = "    Trim me";

console.log("Trimmed start:", anotherText.trimStart());

Output:

Output

Trimmed start: Hello World

Trimmed start: Trim me

Explanation:

In the provided illustration, we have two variables, namely text and anotherText, which are declared with the let keyword and initialized with string values. The trimStart function is utilized to eliminate any leading whitespace from the strings, and the outcomes are displayed in the console. Specifically, text.trimStart eliminates the initial spaces from the text, resulting in 'Hello World', while anotherText.trimStart eliminates the leading spaces in anotherText and outputs 'Trim me'.

String trimEnd in JavaScript

The trimEnd function eliminates any white spaces located at the end of a string.

Example

Example

let text = "Hello World  ";

console.log("Trimmed end:", text.trimEnd());



let anotherText = "Remove spaces at the end    ";

console.log("Trimmed end:", anotherText.trimEnd());

Output:

Output

Trimmed end: Hello World

Trimmed end: Remove spaces at the end

Explanation:

In this instance, we have two variables, namely text and anotherText, which are declared using the let keyword and initialized with string values. The trimEnd function is employed to eliminate any trailing whitespace from the strings, with the outcomes being displayed in the console. Specifically, text.trimEnd eradicates any trailing spaces, yielding 'Hello World', while anotherText.trimEnd does the same, resulting in 'Remove spaces at the end'.

String padStart in JavaScript

The padStart function is utilized to append padding characters at the beginning of a string until it reaches a defined length. This method is commonly employed for tasks such as number formatting or aligning text when displaying output.

Example

Example

let text = "5";

console.log("Padded start:", text.padStart(4, "0"));



let word = "Hi";

console.log("Padded start:", word.padStart(6, "*"));

Output:

Output

Padded start: 0005

Padded start: ****Hi

Explanation:

In the provided illustration, the let keyword is utilized to define two variables, namely text and word, each assigned string values. The padStart function is applied to prepend a character to the beginning of the string, and the outcomes are then displayed in the console.

Consider the function text.padStart(4, "0"), where the number 4 indicates the total length of the resulting string, and the string "0" specifies the content to append at the start of the string. The result of this operation is 0005. In a similar manner, the function word.padStart(6, "") is described with the number 6 indicating the total length of the output string and "" specifying the content to prepend. Consequently, the output would be ****Hi.

String padEnd in JavaScript

The function padEnd appends padding characters to the end of a string using a specified character until it reaches a desired length.

Example

Example

let text = "5";

console.log("Padded end:", text.padEnd(4, "0"));



let word = "Hello";

console.log("Padded end:", word.padEnd(10, "!"));

Output:

Output

Padded end: 5000

Padded end: Hello!!!!!

Explanation:

In the provided illustration, we have declared a pair of variables titled text and word utilizing the let keyword and allocated string values to them. The padEnd function is employed to append characters to the conclusion of a string until it attains the designated overall length, following which the result is displayed in the console.

In the method text.padEnd(4, "0"), the number 4 indicates the overall length of the resulting string, while "0" designates the character used for padding. By adding three zeros to the initial "5", the output becomes 5000. In a similar manner, in the function word.padEnd(10, "!"), the value 10 denotes the total length of the resulting string, with "!" indicating the padding character. As "Hello" is 5 characters long, it is padded with five exclamation marks to yield "Hello!!!!!".

String repeat in JavaScript

The function repeat generates a fresh string containing multiple duplicates of a specified original string. It is useful for creating recurring patterns within strings.

Example

Example

let text = "Hello ";

console.log("Repeated string:", text.repeat(3));



let star = "*";

console.log("Repeated stars:", star.repeat(10));

Output:

Output

Repeated string: Hello Hello Hello

Repeated stars: **********

Explanation:

In this instance, we have declared two variables, namely text and star, using the let keyword, and assigned them string values. The repeat function is employed in this scenario to generate a fresh string by replicating the initial string a defined count of times, and subsequently, the outcomes are displayed on the console. For example, text.repeat(3) generates a novel string by duplicating "Hello" thrice, resulting in the output "Hello Hello Hello." Likewise, star.repeat(10) duplicates the asterisk symbol "" ten times, resulting in the output "*********".

String replace in JavaScript

The replace function is utilized to substitute one value with another within a string. In the absence of a global regular expression, this method only updates the initial occurrence.

Example

Example

let text = "Hello World";

console.log("Replaced string:", text.replace("World", "JavaScript"));



let sentence = "The sky is blue.";

console.log("Replaced color:", sentence.replace("blue", "red"));

Output:

Output

Replaced string: Hello JavaScript

Replaced color: The sky is red.

Explanation:

In the preceding instance, we have declared a pair of variables, namely text and sentence, using the let keyword. We have initialized these variables with string values. The text variable stores 'Hello World' while the sentence variable holds 'The sky is blue'. To swap the initial occurrence of a particular substring with a different substring, we have employed the replace function. Finally, the outcomes are displayed in the console.

In this example, the function text.replace("World", "JavaScript") is explained. It takes two arguments where the first parameter 'World' gets substituted with the second parameter 'JavaScript', resulting in the output 'Hello JavaScript'. Likewise, the function sentence.replace("blue", "red") is illustrated, where 'blue' is replaced by 'red', resulting in the output 'The sky is red'.

String replaceAll in JavaScript

The String class is equipped with a replaceAll function that effectively substitutes all instances of a specified substring with a different value. This feature makes it ideal for carrying out extensive text substitutions.

Note: The replaceAll method is a new method introduced in ECMAScript 2021(ES12). It won't work in older JavaScript environments. It will work perfectly in the ECMAScript 2021(ES12)-supported environment.

Example

Example

let text = "Hello World, World!";

console.log(

  "Replaced all occurrences:",

  text.replaceAll("World", "JavaScript")

);



let repeatedText = "abc abc abc";

console.log("Replace all 'abc':", repeatedText.replaceAll("abc", "xyz"));

Output:

Output

Replaced all occurrences: Hello JavaScript, JavaScript!

Replace all 'abc': xyz xyz xyz

Explanation:

In this instance, we have declared two variables, namely text and repeatedText, using the let keyword. These variables are assigned string values. The method replaceAll is employed in this scenario, requiring two parameters: searchValue and replaceValue. It scans the complete string for every instance of searchValue, substitutes them with replaceValue, and subsequently outputs the outcome to the console.

In the code snippet text.replaceAll("World", "JavaScript"), each instance of 'World' is substituted with 'JavaScript', resulting in the output 'Hello JavaScript, JavaScript!'. Similarly, in the repeatedText.replaceAll("abc", "xyz") example, every occurrence of 'abc' is replaced with 'xyz', producing the output 'xyz xyz xyz'.

String split in JavaScript

The split function is commonly utilized to separate a string into an array by specifying a separator string. This method is frequently employed for parsing comma-separated values or handling input strings.

Example

Example

let text = "Hello,World,JavaScript";

console.log("Split string:", text.split(","));



let sentence = "This is a test sentence";

console.log("Split by spaces:", sentence.split(" "));

Output:

Output

Split string: [ 'Hello', 'World', 'JavaScript' ]

Split by spaces: [ 'This', 'is', 'a', 'test', 'sentence' ]

Explanation:

In this instance, we have declared two variables, namely text and sentence, and initialized them with string values. The split function is employed in this scenario to divide the string into a collection of substrings using a specified separator, following which the outcome is displayed in the console.

In this context, the text.split(",") function divides the string using a comma as the separator, resulting in the output [ 'Hello', 'World', 'JavaScript' ]. On the other hand, sentence.split(" ") is used to separate the string by spaces, leading to the output [ 'This', 'is', 'a', 'test', 'sentence' ].

String indexOf in JavaScript

The indexOf function retrieves the position of the initial appearance of a designated value within a string. It will yield -1 if the value is not located. This function is commonly employed to verify the presence of a particular substring within a text.

Example

Example

let text = "Hello, World!";

console.log("Index of 'Example':", text.indexOf("Example"));



let sentence = "The C# Tutorial Welcomes You.";

console.log("Index of 'Welcomes':", sentence.indexOf("Welcomes"));



let notFound = "Hello Yashraj";

console.log("Index of 'Yashraj':", notFound.indexOf("Yashraj"));

Output:

Output

Index of 'Example': 7

Index of 'Welcomes': 15

Index of 'Yashraj': 6

Explanation:

In the following instance, we define and initialize three variables: text, sentence, and notFound with string values. The indexOf function is employed in this scenario to determine the position of the initial occurrence of a particular substring within a string, and the outcomes are displayed in the console.

In the first scenario, the method text.indexOf("Example") would yield a result of 7. This is due to the fact that the substring "Example" commences at index 7 within the string 'Hello, World!'. Similarly, when sentence.indexOf("Welcomes") is applied, the output will be 15. This is because the term 'Welcomes' initiates at index 15 within the string 'The C# Tutorial Welcomes You'. Conversely, if notFound.indexOf("Pritam") is executed, the return value would be -1. This is because the substring 'Pritam' is not present within the string 'Hello Yashraj'.

String lastIndexOf in JavaScript

The function lastIndexOf provides the position of the final instance of a given value within a string. In cases where the value is not present, the function returns -1. This method is particularly useful for identifying recently duplicated words.

Example

Example

let text = "Hello JavaScript, JavaScript is fun!";

console.log("Last index of 'JavaScript':", text.lastIndexOf("JavaScript"));



let sentence = "The sun sets in the west, and the west is beautiful.";

console.log("Last index of 'west':", sentence.lastIndexOf("west"));



let notFound = "Hello World";

console.log("Last index of 'Python':", notFound.lastIndexOf("Python"));

Output:

Output

Last index of 'JavaScript': 18

Last index of 'west': 34

Last index of 'Python': -1

Explanation:

In this example, three variables are declared as text, sentence, and notFound, and they are initialized with string values. The lastIndexOf function is employed to retrieve the final index of the specified sub-string in a string, and the outcomes are displayed on the console.

In this scenario, the method text.lastIndexOf("JavaScript") will yield the value 18. This is due to the fact that the final instance of 'JavaScript' within the string 'Hello JavaScript, JavaScript is fun!' is positioned at index 18. Similarly, invoking sentence.lastIndexOf("west") will result in 34, as the last appearance of 'west' in 'The sun sets in the west, and the west is beautiful.' is located at index 34. Conversely, when notFound.lastIndexOf("Python") is called, the result will be -1 since the substring 'Python' is absent in the string 'Hello World'.

String search in JavaScript

The search function is employed to look for a particular substring and provide the index of the first occurrence. In case there is no match, it will return -1.

Example

Example

let text = "Hello, JavaScript!";

console.log("Search for 'JavaScript':", text.search("JavaScript"));



let sentence = "The rain in Spain stays mainly in the plain.";

console.log("Search for 'rain':", sentence.search(/rain/));



let caseInsensitive = "Hello World";

console.log(

  "Search for 'hello' (case-sensitive):",

  caseInsensitive.search("hello")

);

console.log(

  "Search for 'hello' (case-insensitive):",

  caseInsensitive.search(/hello/i)

);

Output:

Output

Search for 'JavaScript': 7

Search for 'rain': 4

Search for 'hello' (case-sensitive): -1

Search for 'hello' (case-insensitive): 0

Explanation:

In this instance, three variables - text, sentence, and caseInsensitive - are declared and initialized with string values. The search function is employed to search for a specific substring or a regular expression pattern within a string. It provides the position of the first match, or -1 if no match is detected within the string. Subsequently, the outcomes are displayed in the console.

In the operation text.search("JavaScript"), the method searches for the occurrence of the substring 'JavaScript' within the string 'Hello, JavaScript!'. If a match is found, it returns the starting index of 7 where the match begins. Another example is shown in sentence.search(/rain/) where a regular expression /rain/ is used to find the occurrence of 'rain' within the string 'The rain in Spain stays mainly in the plain.' The first match is identified at index 4.

When utilizing the caseInsensitive.search("hello") function to search for 'hello' within the string 'Hello World', the search is case-sensitive by default, resulting in a return value of -1 if no match is found. Conversely, by employing the caseInsensitive.search(/hello/i) method with the case-insensitive regular expression /hello/i, the search successfully identifies 'Hello' at index 0.

String valueOf in JavaScript

The valueOf function is employed to retrieve the primitive value of an object that is a string. This method is the standard string function in JavaScript.

Example

Example

let x = new String("Example");

let y = new String("Tech");



console.log(x.valueOf(), y.valueOf());

Output:

Output

Example

Explanation:

In this instance, we have two variables, x and y, instantiated through the utilization of the String constructor. This constructor is responsible for generating String objects that hold the values "C# Tutorial" and "Tech", correspondingly. By employing the valueOf method, we extract the underlying primitive string values from these objects. Specifically, x.valueOf yields 'Example' while y.valueOf yields 'Tech'. Consequently, the output displayed in the console will be 'Example'.

Conclusion

JavaScript String methods are highly effective tools for manipulating text. These methods are particularly useful when we need to handle strings effectively, such as in tasks like validating form inputs, parsing text, and formatting data in different programming situations.

The string methods in JavaScript are robust tools for manipulating text data. They are particularly useful when we need to effectively handle strings, such as in tasks like validating form inputs, parsing text, formatting data, and more, across different programming situations.

What is the property utilized to determine the size of a string?

The length property, which is inherent to strings, is employed to determine the length of a string.

In JavaScript, how can one transform a string to uppercase or lowercase?

If there is a need to transform a string to uppercase, the method toUpperCase can be employed. Similarly, when there is a requirement to convert a string to lowercase, the method toLowerCase should be used.

  1. What are the functions that can be used for extracting a substring in JavaScript?

Utilizing the built-in methods like slice, substring, or substr allows developers to extract a specific portion of a string. In JavaScript, how can one go about replacing a substring within a string?

The replace method provided by JavaScript can be employed to substitute a specific substring within a string.

What is the method used in JavaScript to divide a string into an array?

The split function, an intrinsic method in strings, is employed to divide a string in JavaScript.

Input Required

This code uses input(). Please provide values below: