Created
January 22, 2024 16:45
-
-
Save suhailgupta03/04c4da1d3b1f8e51be0f747c8daa4bb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define a string variable | |
const sampleText = "Hello, World! Welcome to JavaScript."; | |
// Use the split function to convert the string into an array | |
// The space character ' ' is used as the separator | |
const wordsArray = sampleText.split(' '); | |
// Log the original string | |
console.log("Original string:", sampleText); | |
// Log the resulting array | |
console.log("Array of words:", wordsArray); | |
/** | |
This code snippet demonstrates how the split function is used to convert a string | |
into an array of substrings in JavaScript. It takes a separator (in this case, a space character ' ') | |
and splits the string at each occurrence of the separator, resulting in an array where each element | |
is a word from the original string. | |
*/ | |
// Define a string variable with underscores | |
const sampleTextWithUnderscores = "user_name_first_name_last_name"; | |
// Use the split function to convert the string into an array | |
// The underscore character '_' is used as the separator | |
const partsArray = sampleTextWithUnderscores.split('_'); | |
// Log the original string | |
console.log("Original string with underscores:", sampleTextWithUnderscores); | |
// Log the resulting array | |
console.log("Array of parts:", partsArray); | |
/** | |
This code snippet shows how the split function can be used with a different separator, | |
the underscore character '_', to split a string into an array of substrings where each element is a part | |
of the original string separated by underscores. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment