03 - String/Array Methods:

03.01 - substring() method:

Code:
//substring() method------------------------------------------------------------------------------------------>
console.log('substring method o/p:');

const str = 'Mozilla';
console.log(str.substring(1, 3));
// expected output: "oz"
console.log(str.substring(2));
// expected output: "zilla"
                

03.02 - indexOf() method:

Code:
//indexOf() method-------------------------------------------------------------------------------------------->
console.log('\n indexOf() method o/p:');

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

// start search from index 2 in the array/string:
console.log(beasts.indexOf('bison', 2));
// expected output: 4

//If the argument isn't present in the array or string:
console.log(beasts.indexOf('giraffe'));
// expected output: -1
                

03.03 - replace() method:

Code:
//replace() method------------------------------------------------------->
console.log('\n replace method o/p:');
                        
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
                        
                        
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
                

03.04 - replaceAll() method:

Code:
//replaceAll() method------------------------------------------------------->
console.log('\n replaceAll method o/p:');
                    
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replaceAll('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"


// global flag required when calling replaceAll with regex
const regex = /fox/ig;
console.log(p.replaceAll(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

Note: when using a `regexp` you have to set the global ("g") flag; otherwise, 
it will throw a TypeError: "replaceAll must be called with a global RegExp".                    
                

03.05 - repeat() method:

Code:
//repeat() method----------------------------------------------------------------------------------------->
console.log('\n repeat method o/p:');
                    
const chorus = 'Because I\'m happy. ';
                    
console.log(`Chorus lyrics for "Happy": ${chorus.repeat(5)}`);
                    
// expected output: "Chorus lyrics for "Happy": Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy. Because I'm happy."
                

03.06 - slice() method:

Code:
//slice() method------------------------------------------------------------------------------------------>
console.log('\n slice method o/p:');

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
                

03.07 - splice() method:

Code:
//splice() mehtod------------------------------------------------------------------------------------------>
console.log('\n splice method o/p:');

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
                

03.08 - split() method:

Code:
//split() mehtod------------------------------------------------------------------------------------------>
console.log('\n split method o/p:');

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"

const chars = str.split('');
console.log(chars[8]);
// expected output: "k"

const strCopy = str.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]
                

03.09 - join() method:

Code:
//join() mehtod------------------------------------------------------------------------------------------>
console.log('\n join method o/p:');

const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"