JavaScript Learning Strings_Properties Method and Template Literals

 


Strings are useful for holding data that can be represented in text form. You are use single quotes (' ') or double quotes (" ") to wrap a string literal. In Es6 you create a template literal by wrapping your text in back ticks

let simple = `This is your text `;
            // eg. 01
            let firstName ='Pradeep', lastName= 'Bhosle';
            let fullName = `Hi your firstname is ${firstName} and last name is ${lastName}`;
            console.log(fullName);
            // Output: Hi your firstname is Pradeep and last name is Bhosle

JavaScript String Method

  1.  charAt(2): Its return the character at the "2" position with in the string.
    let myName = "Pradeep Bhosle"; // 0P 1r 2a
            console.log(myName.chrAt(2));
            // Output:  a 
  2.  concat(a1,a2): Combines one or more string and return the concatenate string.
    let a1 = "Pradeep Bhosle"; 
                let a2 = " Is Working on JavaScript.";
                let c = a1.concat(a2);
                console.log();
                // Output: Pradeep Bhosle Is Working on JavaScript. 
  3. indexOf(substr, [start_from]):returns the index number of the searched character within the string. If not found, it will return -1
    let text1 = "Hi, this is first para";
                let text2 = text1.indexOf("is");
                console.log(text2);
                // Output: 6;
                let text1 = "Hi, this is first para";
                let text2 = text1.indexOf("of"); // of is not present on string.
                console.log(text2);
                // Output: -1;
  4. lastindexOf(substr, [start_from]):returns the index number of the searched character within the string. This method will return the index of the last occurrence of a specified text in a string
    let myPrintvalue = 'javascript Node.js';
                console.log(myString.lastIndexOf('N'));
                //output: 11
  5. slice(substr, [end]):This method returns a sub string of the string based on the "start" and "end" index , it will not include the "end" index itself. "End" argument is optional, and if none is specified, the slice includes all characters from "start" to end of string.
    let text="programming"
                var mystr= text.slice(0,4)
                console.log(mystr)
                //Output:- "prog"
    
                // toLowerCase()
                text.toLowerCase();
    //toUpperCase() text
    .toUpperCase();



Comments