split a string into two strings in javascript

To split a string into two strings in Javascript, there are a few ways to achieve this.

  1. Using the split() method:
index.tsx
const fullString = "Hello World";
const splitStrings = fullString.split(" ");
console.log(splitStrings);

// Output: ["Hello", "World"]
136 chars
6 lines
  1. Using the substr() or substring() method:
index.tsx
const fullString = "Hello World";
const firstString = fullString.substr(0, 5);
// or const firstString = fullString.substring(0, 5);
const secondString = fullString.substr(6);
// or const secondString = fullString.substring(6);

console.log(firstString);
console.log(secondString);

// Output:
// "Hello"
// "World"
316 chars
13 lines

gistlibby LogSnag