OneBite.Dev - Coding blog in a bite size

split a string by a delimiter in Javascript

Code snippet on how to split a string by a delimiter in Javascript

  const str = 'Hello, world';
  const delimiter = ',';

  const strSplit = str.split(delimiter);
  console.log(strSplit) // Output: ['Hello', ' world'] 

In this example, the string “Hello, world” is first declared and stored in a variable called “str”. A delimiter of ’,’ is then declared and stored in the variable “delimiter”. The string is then split by the delimiter using the split function and stored in a variable called “strSplit”. The split function divides the string into an array with the delimiter as the boundry, in this example the output would be ‘Hello’ and ’ world’. Finally the result is logged to the console.

javascript