String in ES6

String in ES6

The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like a person’s name or description of a product.

In ES6, if we write ‘aby’ text within the single (‘’) or double quotes (“”) both are referred as a string.

Here, we have the following two ways to define a string-

  • With the help of string object (using the new keyword)
  • With the help of string literal

We will try to describe the above ways in detail-

With the help of string object

We can create a string with the help of the new keyword. The syntax for the same is given below-

Syntax

var stringname = new string (“String Literal”);

With the help of string literal

We can also define a string with the help of a single (‘’) or double quotes (“”). Here, we have a syntax for defining the string using a string literal.

Syntax

var stringname = “string value”;

String Methods in ES6

In ES6, we have the following four-string methods. They are given below in the tabular form-

Sr. No.            Method          Description
               1.          includes ()This method returns true if the particular argument lies inside the string.
               2.          repeat ()This method is used to return a new string, based on repeated argument count.
               3.          startsWith ()It is used to determine that the string starts with the characters of a particular string.
               4.          endWith ()This method determines that the string ends with characters of a particular string.

Now, we are going to discuss the above methods in a detailed form.

includes() Method

This method is used to define that the string consists of the characters of a particular string or not. It is a case-sensitive method. The method returns the true; if the string involves the characters; otherwise, it returns the false.

Syntax

string.includes(searchValue, start)

The above syntax contains the following parameter.

searchValue: It is the substring of searching. It is a necessary parameter to use.

start: This parameter presents the address fro, where to start searching inside the string. It contains value 0 by default.

Example

Here, we have an example to understand the above method.

let string = “Welcome to ES6”
console.log (string.includes(‘Welcome’, 6));
console.log (string.includes(‘Welcome’, 8));

Output

After the execution of the above code, we got following output:

String in ES6

repeat() Method

The repeat() method is used to define a new string that includes the particular number of copies of the string.

Syntax

string.repeat(count)

The above syntax contains a single parameter.

Count: This parameter is used to display the repetition of the string. It is a needed parameter. Its range is 0 to infinity.

Example

Here, we have an example to understand the repeat() method.

var string = “Hello ES6”;
console.log(string.repeat(3));

Output

After the execution of the above code, we got the following output:

String in ES6

startsWith () Method

The startsWith() method defines that the string starts with a particular string character or not. It returns the true if the string starts with string character; otherwise, it returns false.

Syntax

String.startsWith(searchValue, startPosition)

The above syntax has two following parameters:

searchValue: It refers to those characters which are searched for the starting position of a string. It is a necessary parameter.

startPosition: It refers to the specified position, where we start searching. The default value of this parameter 0. It is an optional argument.

Example

var string = “(Welcome to ES6 :)”;
console.log(string.startWith(‘Wel’, 0));
console.log(string.startWith(‘Wel’, 0));

Output

After the execution of the above example, we got the following output:

String in ES6

endWith () Method

This method is used to define that the string ends with a particular string character or not. It is also case-sensitive.

Syntax

String.endsWith(searchValue, length)

The syntax consists of the following two parameters.

searchValue: It refers to those characters which are searched for the ending position of a particular string. It is a needed parameter.

length: It defines the length of the searched string. It is an optional parameter. If we miss this parameter, then the method searches for the full string.

Example

var string = “Welcome to the world of ES6.”;
console.log (string.endsWith(“to”, 10));
console.log (string.endsWith(“To”, 10));

Output

After the successful execution of the code, we got the following output:

String in ES6

Properties of String

Here, we have the following properties of the string, which are tabulated below-

            Sr. No.             Property         Description
                1.             prototypeThis property is used to enable us to associate the properties and the methods to an enduring object.
                2.             constructorIt is used to return the constructor function for the object.
                3.              LengthThis property returns the length of the object.

Now, we discuss the properties of string in detail.

String Prototype Property

This property facilitates us to associate the properties and methods with an enduring object type. It is a global property used with all the objects.

Syntax  

Object.prototype.name = value;

Example

Here, we have an example to understand the above property.

function employee (name, section)
{
          this.name = name;
          this. section = section;
}
employee.prototype.age = 35;
var emp = new employee (‘Robert’, ‘Security’);
console.log(emp.name);
console.log(emp.section);
console.log(emp.age);

Output

After the execution of the above example, we got the following output:

String in ES6

String constructor property

The String constructor property is used to return the constructor function for an object. This property returns the function of the function instead of the function name.

Syntax

String.constructor

Example

Here, we discuss the string constructor property with the help of the following example.

var string = new String(“Hello JavaScript”);
console.log(“The value of string.constructor is: ” +string.constructor);

Output

We got the following output after the successful execution of the code.

String in ES6

String length Property

This property is used to return the length of the string (number of constructors).

Syntax

string.length

Example

Here, we have an example to understand the above property.

var string = new String(“Welcome to ES6”);
console.log(“The number of characters in a string: ”+string.length);

Output

After the execution of the code, we got the following output:

String in ES6

String Methods in JavaScript

We have some string methods used in JavaScript that are given below in the tabular form.

             Sr. No.          Method          Description
                 1.       charCodeAt()This method gives the Unicode character value, lies at a particular address.
                 2.       charAt()It is used to return the char value, presented at a specified index (address).
                 3.       match()This method is used to search a regular expression in an existing string and it returns that if there should be a match.
                 4.       concat()It provides consolidation of multiple string.
                 5.       indexOf()It gives us the char value position in the given string.
                 6.       toString()This method returns the string which shows a particular object.
                 7.      lastIndexOf()It searches the character from last position and returns the char value address in the string.
                 8.       replace()This method replaces the existing string with a particular replacement. 
                 9.       valueOf()The valueOf() method returns the primitive value of string object.
                 10.         split()It divides the single object into different string arrays by converting strings to substrings.
                 11.         slice()This method is used to extract a section of string and gives the new string.
                 12.        substr()The substr() method is used to fetch the section of the string. It is based on the starting address and the particular length.
                 13.        toLowerCase()It is used to transform the existing string into lower case characters.
                 14.        toUpperCase()It is used to transform the existing string into upper case characters.
                 15.         trim()It helps us to remove the white space from the left and right side of the string.
                 16.         substring()It fetches the section of the existing string that depends on a particular position.
                 17.   toLocaleLowerCase()It transforms the string into lower case according to the current locale host.
                 18.   toLocaleUpperCase()It transforms the string into upper case according to the current locale host.
                 19.            search()This method is used to search a particular regular expression and gives its address if the condition is matched.

Related Topics

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.