Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Javascript string methods with examples

In the chapter, we will learn javascript string methods with examples. The javascript has inbuilt methods for string manipulation with almost cases. Let’s write examples and list the same.

Javascript string methods:

let string = 'Learn Javascript';
    let stringObj = new String('Learn Javascript');
    let nfcString = '\u0041\u006d';

    const stringInJS = function(...param) {
        for (var i = 0; i < param.length; i++)
        console.log(param[i]);
    };

    stringInJS(
        string.charAt(2), 
        // prints : h
        
        string.charCodeAt(2), 
        // prints : 97
        
        string.concat(' ', 'Michel'), 
        // prints : Learn Javascript Michel
        
        string.includes('Javascript'), 
        // prints : true
        
        string.endsWith('Javascript'), 
        // prints : true
        
        string.endsWith('Javascript', 2), 
        // prints : false
        
        string.lastIndexOf('r'), 
        // prints : 30
        
        string.match(/[A-Z]/g), 
        // prints: ["L", "J"]
        
        string.padEnd(20, '.'), 
        // prints : Learn Javascript....
        
        string.padStart(20, '*'), 
        // prints : ****Learn Javascript
        
        string.repeat(2), 
        // prints : Learn JavascriptLearn Javascript
        
        string.replace(/Javascript/gi, 'programming'), 
        // prints : Learn programming
        
        string.search(/[A-Z]/g), 
        // prints : 0
        
        string.slice(5), 
        // prints : Javascript
        
        string.slice(5, 10), 
        // prints : Java
        
        string.slice(-5), 
        // prints : cript
        
        string.slice(-10, -5), 
        // prints : Javas
        
        string.split(' '), 
        // prints : ["Learn", "Javascript"]
        
        string.startsWith('Learn'), 
        // prints : true
        
        string.substr(0,4), 
        // prints : Lear
        
        string.substring(6,10), 
        // prints : Java
        
        string.toLocaleLowerCase('en-US'), 
        // prints : learn javascript
        
        string.toLocaleUpperCase('en-US'),
        // prints : LEARN JAVASCRIPT

        string.toLowerCase(),
        // prints : learn javascript

        string.toUpperCase(),
        // prints : LEARN JAVASCRIPT

        stringObj,
        // prints : {"Learn Javascript"}

        stringObj.toString(),
        // prints : Learn Javascript

        string.trim(),
        // prints : Learn Javascript

        string.trimStart(),
        // prints : Learn Javascript

        string.trimEnd(),
        // prints : Learn Javascript

        nfcString.normalize('NFC')
        // prints : Am
    );