Welcome to DavisDay.com! Enjoy Your Visit!

String Methods

This article will describe four of JavaScript's string methods.


JavaScript String Methods - What Are They

To begin, a string in JavaScript expresses data in the form of text. A JavaScript string is a data type that is built into the program. The value of a string can only have a property or method in JavaScript, because this language treats the values like objects.


The 'toString()' Method

The toString() method is used for conversion and concatenating. It may the change the return value of a variable to a string (or text). The parameters for this method are usually left empty, although, custom parameters may be entered to override the orignal use of the method.

            
                let num = 26;
                let text = num.toString();
                console.log(text);
            
        

The variable ends up with a string data type


The 'valueOf()' Method

The valueof() method is used to return the value of the object being called, or the object itself. This method may convert an object to a primitive value, which is a value that cannot be changed. The paramter takes the name of the variable being instantiated.

            
                let string = "Hello World!";
                console.log(string.valueOf(string));
            
        

The logged output will display "Hello World!" (However, without the quotes). This would be a string data type


The 'toLowerCase()' Method

The toLowerCase() method will make the target variable appear with lowercase text. The parameter is left empty for this method.

            
            let lwcSandwich = "mcChicKen";
            lcSandwich = lwcSandwich.toLowerCase();
            console.log(lcSandwich);
            
        

The result will change from 'mcChicKen' to 'mcchicken', without the quotes, being a string data type


The 'toUpperCase()' Method

The toLowerCase() method will make the target variable appear with uppercase text. The parameter is left empty for this method.

            
            let upcSandwich = "MCCHICKEN";
            ucSandwich = upcSandwich.toUpperCase();
            console.log(ucSandwich);
            
        

The result will change from 'mcChicKen' to 'MCCHICKEN' without the quotes, being a string data type