// stringMethods.js // Example string for demonstration const str = " Hello World "; // 1. charAt(index): Returns the character at the specified index console.log('1. charAt(1):', str.charAt(1)); // Output: "e" // 2. concat(string1, string2, ...): Combines two or more strings const str2 = " from JavaScript!"; console.log('2. concat():', str.concat(str2)); // Output: " Hello World from JavaScript!" // 3. includes(searchString, position): Checks if the string contains the specified substring console.log('3. includes("World"):', str.includes("World")); // Output: true // 4. indexOf(searchValue, fromIndex): Returns the index of the first occurrence of a specified value console.log('4. indexOf("o"):', str.indexOf("o")); // Output: 4 // 5. lastIndexOf(searchValue, fromIndex): Returns the index of the last occurrence of a specified value console.log('5. lastIndexOf("o"):', str.lastIndexOf("o")); // Output: 7 // 6. slice(start, end): Extracts a section of the string console.log('6. slice(0, 5):', str.slice(0, 5)); // Output: " Hel" // 7. split(separator, limit): Splits the string into an array of substrings console.log('7. split(" "):', str.split(" ")); // Output: ["", "", "Hello", "World", "", ""] // 8. toLowerCase(): Converts the string to lowercase console.log('8. toLowerCase():', str.toLowerCase()); // Output: " hello world " // 9. toUpperCase(): Converts the string to uppercase console.log('9. toUpperCase():', str.toUpperCase()); // Output: " HELLO WORLD " // 10. trim(): Removes whitespace from both ends of the string console.log('10. trim():', str.trim()); // Output: "Hello World"
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter