string slicing creating a substring from a portion of another string

  • string.slice(start , end)

A simple example:

const fullName = "Bro Code";
 
let firstName = fullName.slice(0 , 3);// output should be "Bro"
let lastName = fullname.slice(4 , 8); //output should be "Code"
let firstChar = fullname.slice(0 , 1); //output should be "B"
let lastChar = fullname.slice(-1); //output should be "e"

Example with .indexof()

const fullName = "Broseph Code"
 
let firstName = fullName.slice(0 , fullName.indexOf(" "));
let lastName = fullName.slice(fullName.indexOf(" ") + 1);
 
console.log(firstName);
console.log(lastName);

Example to trim a email:

const email = "Bro1@gmail.com"
 
let username = email.slice(0 , email.indexOf("@"))
let extension = email.slice(email.indexOf("@") + 1);