The built-in function split() and slice().

split() and slice() are JavaScript built-in function. These built-in functions are set of commands or piece of code which it's functionality is pre-defined in a program or programming framework. These built-in functions perform a wide range of operations and simplifies complex operations into a simple step. other examples of JavaScript built-in functions are:

filter(), map(), splice(), Date(), concat(), join(), sort() etc...

THE SPLIT() FUNCTION

As the name implies, the split() function is used to split string object into a substring array and returns new array object. the split() method accept two arguments as a value passed into the function. Just as below: split(separator, limit)

Arguments for split() method

- Argument one: Separator this is a required argument passed into the split function, it helps to specify the character to use for separating the objects of the string. but in a situation where the separator is not declared or omitted the split method returns an array of 1 elements consisting the string objects.

- Argument two: Limit the limit parameter is used to set an end point to the split method.

how to use the split() function

1. Splitting by not declaring the separator argument

var students = "Tolu Bose Abraham Bashir";
var splitValue = names.split();
console.log(splitValue);

// ["Tolu Bose Abraham Bashir"]

the example above return an array of one element because the separator which will be used to separate the string object is not declared.

2. Splitting using double/single quote without whitespace as an argument.

var greet = "How are you";
var splitGreet = greet.split("");
console.log(splitGreet);

/* ["H","o","w"," ","a","r","e"," ","y","o","u"] */

using the double quote, the split method splits each character including whitespaces in the string objects.

3. Splitting every word in the string object by using whitespace surrounded with quote.

var comment = "Good job";
var splitComment = greet.split(" ");
console.log(splitComment);

/* ["Good", "job"] */

NB: for you to split each word, you need to add whitespace in the double/single quote

4. Splitting using a character as a separator argument

var remind = "make sure you sleep well";
var splitRemind = greet.split("e");
console.log(splitRemind);

/* ["mak", "sur", "you sl", "pw", "ll" ] */

in this process, the split method loop and check through the string objects and split it every time it finds the specified separator.

5. Splitting by specifying separator using regex pattern as an argument regex pattern can also be used as a separator parameter inside the split method.

var greet = "How are you";
var splitGreet = greet.split(/o/);
console.log(splitGreet);

// ["H", "w are y", "u"]

In the example above a regex pattern is used as a separator by putting the seperator value in between two forward slash, this works the same as using a character surrounded with double or single quote.

6. Splitting by specifying end point using the limit argument

var good = "I am doing good";
var splitGood = greet.split(" ", 2);
console.log(splitGood);

/* ["I", "am"] */

The splitting process stops immediately it counts to the index of 2. Negative values can be used to count from the end of the string.

THE SLICE() FUNCTION

slide is one of the built-in JavaScript commands that returns array elements from the up until just before the end specifiers. slice method can also be used to copy and paste by not passing any arguments.

slice arguments

slice(start point, end point)

- Argument one: Required it's an integer that specifies where to start the selection.
- Argument two: Optional it's an integer that specifies where to end the selection but does not include. if this parameter is ignored all elements from the start position to the end will be selected.

Note: use negative number to select from the end of an array.

How to use the slice() method

1. Slicing by ommiting the first parameter

var number = [3,2,5,6,7,1,5];
var sliceNum = number.slice();
console.log(sliceNum);

// [3,2,5,6,7,1,5]

This copy and paste the array by returning the exact value of the array elements.

2. Slicing by specifying start point

var number = [3,2,5,6,7,1,5];
var sliceNum = number.slice(3);
console.log(sliceNum);

// [ 6,7,1,5]

3. Slicing by specifying start and end point

var number = [3,2,5,6,7,1,5];
var sliceNum = number.slice(2, 5);
console.log(sliceNum);

// [ 5,6,7]

Thanks for reading.