String Manipulation in Javascript | Javascript

 // Concatenation
const concatenatedString = 'Hello' + ' ' + 'World';

// Trimming
const trimmedString = '   Trimmed   '.trim();

// Searching
const index = 'Hello World'.indexOf('World');
 

AI Explanation

This JavaScript code snippet demonstrates three common string operations: 1. Concatenation: - The `concatenatedString` variable is created by concatenating three strings 'Hello', ' ' (space), and 'World' using the `+` operator. - It results in the value 'Hello World'. 2. Trimming: - The `trimmedString` variable is created by calling the `trim()` method on the string ' Trimmed '. - The `trim()` method removes whitespace from both ends of the string. - It results in the value 'Trimmed'. 3. Searching: - The `index` variable is created by calling the `indexOf()` method on the string 'Hello World' to find the index of the substring 'World'. - The `indexOf()` method returns the index at which the substring was found or -1 if it is not found. - In this case, 'World' is found starting at index 6. - So, the value of `index` would be 6. These operations are common string manipulations that can be useful in various scenarios while working with strings in JavaScript.