Skip to content
Related Articles

Related Articles

ES6 | String

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 28 Mar, 2022

There are two string types string primitives and string objects in ES6 JavaScript. It wraps up the JavaScript’s string primitives by the helper methods. There are a few numbers of helper methods.

  • string primitives: A primitive gets auto-boxing to its wrapper type when a method of the wrapper type is invoked. It has no helper methods, it is nothing more than a pointer to a raw data memory reference. So here it wraps to a string wrapper. (var i = 1) here it wraps to an integer wrapper.
  • string objects: This is a String Object. The String object is created with the help of String Class. It wraps string primitive data type with a number of helper methods.

Syntax: There are three properties in ES6 JavaScript mentioned and described below: 

var stringobject = new String(string);

String Properties:

ConstructorThis property returns a reference to the string function which created the instance prototype.
LengthThis property returns the length of the string.
PrototypeThis property a global property which is available with almost all the objects. This allows you to add properties and methods to an object, where the prototyped object has different behavior compared to string primitive auto-boxing.

Below example illustrates the properties:



<!DOCTYPE html>
        function hero(id, name) {
   = id;
   = name;
        // Object e
        var e = new hero(123, "GeeksforGeeks");
        // Object e1
        var e1 = new hero(124, "Courses");
        // This prototype property is constant for all objects. = "Edutech";
        // Returns object reference
        document.write("Constructor Property = " + e.constructor);
        // Returns length
        document.write("<br>Length of = " +;
        // Returns the new property(group)
        document.write("<br>Prototype( group = " +;
        // Returns the new property(group)
        document.write("<br>Prototype( group = " +;


Constructor Property = function hero(id, name) { = id; = name; }
Length of = 13
Prototype( group = Edutech
Prototype( group = Edutech

Methods of string Objects: These are the list of methods available in a String object. 

charAt(character)This methods returns the character at the specified index.
charCodet(character)This methods returns the ascii code of the character at the specified index.
concat(string)This methods returns the concatenated string.
indexOf(string)This methods returns the starting index of the given string.
lastIndexOf(string)This methods returns the starting index of the given string’s last occurrence.
localeCompare(string)This methods returns 0, if the compared strings the equal, return 1 or -1 if the compared strings are not equal and 1 and -1 depending on the sorted order.
match(RegExp, string)returns the match of the RegExp.
replace(RegExp, string)returns the replaced RegExp in the string.
search(RegExp)This methods searches for the given word or regex. If a string is passed as a parameter it gets converted into RegExp by using new RegExp(obj).
slice(startIndex, endIndex)This methods returns the string from the startIndex to endIndex(excluding) at the specified index.
substr(startIndex, length)returns the string from the startIndex to startIndex+length(excluding) at the specified index.
substring(startIndex, endIndex)returns the string from the startIndex to endIndex(excluding) at the specified index.
split(“separator”, number of splits)returns an array which got splited with the separator and given b=number of splits, if splits are not mentioned then return the whole array.
toLocalLowerCase()returns a string in lowercase with the current locale.
toLocalUpperCase()returns a string in uppercase with the current locale.
toLowerCase()returns the calling string value converted to lowercase.
toupperCase()returns the character at the specified index.
toString()returns the calling string value converted to uppercase.
valueOf()returns the primitive value of a String object.

Below example will illustrate the methods: 



<!DOCTYPE html>
        var str = new String("A Online Computer Science Portal");
        var str1 = new String("Computer Science Portal for Geeks");
        document.write("str.charAt(0) is = " + str.charAt(0));
        document.write("<br>str.charCodeAt(0) is = " + str.charCodeAt(0));
        document.write("<br>str1.concat('=>Geeks?') = " + str1.concat('=>Geeks?'));
        document.write("<br>str.indexOf('of') = " + str.indexOf('of'));
        document.write("<br>str.lastIndexOf('of') = " + str.lastIndexOf('of'));
        document.write("<br>str.localeCompare( 'A Online Computer Science Portal') = "
                       + str.localeCompare('A Online Computer Science Portal'));
        document.write("<br>str.localeCompare( 'Computer Science Portal for Geeks') = "
                       + str.localeCompare('Computer Science Portal for Geeks'));
        document.write("<br>str.localeCompare( 'A Computer Science Portal') = "
                       + str.localeCompare('A Computer Science Portal'));
        if ("Geeksfor") != -1) {
            document.write("<br>Geeks exist in str.");
        } else {
            document.write("<br>Geeks doesn't exist in str.");
        document.write("<br>str.slice(5, -1) = " + str.slice(5, -1));
        document.write("<br>str.substr(2, 9) = " + str.substr(2, 9));
        document.write("<br>str.substring(2, 9) = " + str.substring(2, 9));
        document.write("<br>str.split(' ') = " + JSON.stringify(str.split(' ')));
        document.write("<br>str.split(':', 4) = " + JSON.stringify(str.split(':', 4)));
        document.write("<br>str.split('of', 4) = " + JSON.stringify(str.split('of', 4)));
        document.write("<br>str1.toLocaleLowerCase( ) = " + str1.toLocaleLowerCase());
        document.write("<br>str1.toLocaleUpperCase() = " + str1.toLocaleUpperCase());
        document.write("<br>str1.toLowerCase( ) = " + str1.toLowerCase());
        document.write("<br>str1.toUpperCase() = " + str1.toUpperCase());
        document.write("<br>str1.toString() = " + str1.toString());
        document.write("<br>str1.valueOf( ) = " + str1.valueOf());


str.charAt(0) is = A
str.charCodeAt(0) is = 65

str1.concat('=>Geeks?') = Computer Science Portal for Geeks=>Geeks?

str.indexOf('of') = -1
str.lastIndexOf('of') = -1

str.localeCompare( 'A Online Computer Science Portal') = 0
str.localeCompare( 'Computer Science Portal for Geeks') = -1
str.localeCompare( 'A Computer Science Portal') = 1

Geeks doesn't exist in str.

str.slice(5, -1) = online Computer Science Portal
str.substr(2, 9) = Online Co
str.substring(2, 9) = Online

str.split(' ') = ["A","Online","Computer","Science","Portal"]
str.split(':', 4) = ["A Online Computer Science Portal"]
str.split('of', 4) = ["A Online Computer Science Portal"]

str1.toLocaleLowerCase( ) = computer science portal for geeks
str1.toLowerCase( ) = computer science portal for geeks

str1.toString() = Computer Science Portal for Geeks
str1.valueOf( ) = Computer Science Portal for Geeks

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!