DEV Community

Chandra Prakash Pal
Chandra Prakash Pal

Posted on

Create function to check a character exist in any string or not using string prototype.

Here i will discuss to add a function in string object using prototype, so can access every where in application.

As we know that prototype is super object in javascript.

String.prototype.checkExist = function (charStr) {
    const str = this.valueOf();
    let exitsCheck = false;
    const strArr = charStr.split('');
    for (let i = 0; i < strArr.length; i++) {
        if (str.includes(strArr[i])) {
            exitsCheck = true;
            break;
        }
    }
    return exitsCheck;
}
Enter fullscreen mode Exit fullscreen mode

In above function checkExist function will check that a character exist in a string or not,if exists the return true other wise return false.

Reference: https://jsfiddle.net/u6BER/

Top comments (0)