JavaScript String | String Object, Example

A string in JavaScript is a sequence of Unicode characters. It is simply an array of characters. Each character takes 16 bits.

Each character in the string has its position or index. We can access it by its index.

The indexing of a string, like that of an array, is zero-based. The index of the first character of a string is zero.

A string is enclosed within a pair of double or single quotation marks. Both are valid ways to represent a string in JavaScript.

Characters enclosed in the quotes may be zero or more. Let’s see the following examples.

let name1 = "John"; // Double quotes
let name2 = 'Bob'; // Single quotes.

We can use quotation marks inside a string, as long as they don’t match the quotes surrounding the string. Consider the following example.

let message1 = "I love 'JavaScript programming'";
let message2 ='I love "JavaScript programming"';

String Object in JavaScript


In JavaScript, string is an object. Any variable whose value is a string is actually a string object. That is, JavaScript stores a string value as a string object in a variable.

The string object in JavaScript is a wrapper around the string primitive value. It is the most commonly used object in JavaScript.

Ways to create String in JavaScript


There are two ways to create string in JavaScript. They are as follows:

  • By string literal
  • By string object (using new keyword and String() constructor)

Let’s understand each way one by one with the help of examples.

By string literal


We create a string literal using either a single quotes or double quotes. The general syntax to create a string object using string literal is as follows:

let stringname="string value";

Let’s take a simple example of creating string literal.

<script>
  let str = "This is string literal"; // creates a string literal value.
  console.log(str);
</script>
Output:
      This is string literal

Most JavaScript programmers use single quotes in the string value and double quotes on the outside of string value like this:

let message = "Every people love his 'country' in the world";

By string object (using new keyword)


We can also create a string as an instance of String object by using new keyword and String() constructor. The general syntax of creating string object using new keyword is as follows:

stringObjectName = new String(string);

Here, stringObjectName is the name of object we are creating. The String() constructor parameter area contains string literal in either single quotes or double quotes.

Strings created through this constructor function are considered objects in JavaScript. Consider the following example.

let str = new String("Hello JavaScript");

Here, we have created an instance of String object for the string “Hello JavaScript”. If we find its data type using typeof operator, we will find that it is an object, not a string.


Let’s take an example program in which we will create a string in JavaScript by new keyword and String function().

<script>
  let str = new String("Welcome to Scientech Easy");
  console.log(str);
</script>
Output:
    Welcome to Scientech Easy

Note: Although, creating a string in JavaScript using new keyword is not recommended because it consumes more memory and slow down the execution speed. Strings objects may produce unexpected result.

String Length (Property of String object)


The most common property of String object in JavaScript is “length” property. This built-in property tells us how many characters, including spaces and punctuation marks, are in the string.

The general syntax to find the length of JavaScript string is as:

stringObjectName.length;

Example: Determining the length of Strings


Let’s create a JavaScript program to determine the length of strings using its length property.

<script>
// Create string literals.
   let str1 = "abcde";
   let str2 = "a b c d e";
   let str3 = "\"abcde\"";

// Determine the length of strings.
   let strLength1 = str1.length;
   let strLength2 = str2.length;
   let strLength3 = str3.length;

// Print the length of strings.
   console.log(strLength1);
   console.log(strLength2);
   console.log(strLength3);
</script>
Output:
      5
      9
      7

As you can see in the above output, all characters, including spaces and punctuation marks within strings, are counted towards the length.


Note: For a null string, the length property is zero.

Escape Character


When creating a string in JavaScript, we cannot add directly several special characters to the string. To add these special characters in strings, JavaScript provides a set of escape codes that are as below in the table:

Table: String object escape codes

EscapeDescriptionExampleResult
\’Single quote mark“willn\’t be”willn’t be
\”Double quote mark“I \”think\” I \”am\””I “think” I “am”
\\Backslash“a\\b\\c”a\b\c
\nNew line“One\nTwo”One
Two
\rCarriage return“One\rTwo”One
Two
\tHorizontal tab space“One\ttwo\tThree”One Two Three
\bBackspace“Correctoin\b\b\bion”Correction

As you can see in the above table, all special escape characters begin with backslash (\). Let’s take an example program based on the escape sequence character.

<script>
// Create string literals.
   let str1 = "willn't be";
   let str2 = "I \"think\" I \"am\"";
   let str3 = "One\\Two\\Three";
   let str4 = "This is\na string";
   let str5 = "One\tTwo\tThree";

   console.log(str1);
   console.log(str2);
   console.log(str3);
   console.log(str4);
   console.log(str5);
</script>
Output:
      willn't be
      I "think" I "am"
      One\Two\Three
      This is
      a string
      One Two Three

JavaScript String Example Programs


Let’s take some important examples based on the string literal and string object.

Example 1: When we use == operator, str1 and str2 are equal.

<script>
  let str1 = "I am string"; // create string literal.
  let str2 = new String("I am string"); // create a string object.
  let s = str1 == str2;
  console.log(s);
</script>
Output:
      true

Example 2: When we use === operator, str1 and str2 are not equal.

<script>
  let str1 = "I am string";
  let str2 = new String("I am string");
  let s = str1 === str2;
  console.log(s);
</script>
Output:
     false

Comparing JavaScript String objects using == and === Operators


Let’s create a JavaScript program to compare two string objects using == operator.

<script>
  let str1 = new String("Hello");
  let str2 = new String("Hello");
  let s = str1 == str2;
  console.log(s);
</script>
Output:
      false

Let’s create a JavaScript program to compare two string objects using === operator.

<script>
   let str1 = new String("Hello");
   let str2 = new String("Hello");
   let s = str1 === str2;
   console.log(s);
</script>
Output:
      false

Note: Comparing two string objects in JavaScript always returns false values.

prototype Property of String object


This property allows us to add properties or methods to the string objects on the page. The following script code demonstrates an example.

<script>
// This statement adds a property with a value to the string object.
   String.prototype.attitude = "cool";
// Create a new instance of String object.
   let str = new String("John");
   console.log(str.attitude); // We have used the new property with the instance of string object.
</script>
Output:
     cool

In this example program, we have used prototype property to add a property “attitude” to the string object. Hence, the string object “John” has an attitude property of “cool”! This is the second property of String object.

constructor Property of String object


This constructor property holds the value of the constructor function for an instance of an object. To use the constructor property, we need to create an instance of String object rather than a literal. The following code demonstrates an example.

<script>
// Create a new instance of String object.
   let str = new String("Hello JavaScript");
// This statement will print the value of constructor property on the web page.
   console.log(str.constructor);
</script>
Output:
      ƒ String() { [native code] }

Note: The String object in JavaScript has only three properties: length, prototype, and constructor. We can use length property with both string literals and string objects.

But, to use the properties prototype and constructor, we need string objects. That is, we cannot use the prototype and constructor properties with string literals.


In this tutorial, you learned about the Strings in JavaScript with the help of examples. Hope that you have understood how to create strings using creating string literals and string objects.

Strings are an important chapter of any programming language. In the next tutorial, we will discuss the methods of String object.
Thanks for reading!!!