Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, July 17, 2024

Javascript String extend for new function.

Javascript String extend for new function.
First Approch:
String.prototype.toArray=function(){
   let arr=[];
   for(let ch of this){
    arr.push(ch);
	}
	return arr;
}

let name="suresh";
console.log(name.toArray()); //output   [ 's', 'u', 'r', 'e', 's', 'h' ]

Second Approch:
String.prototype.toArray=function(){
	return this.split('');
}

let name="suresh";
console.log(name.toArray()); //output   [ 's', 'u', 'r', 'e', 's', 'h' ]
Share: