The Wayback Machine - https://web.archive.org/web/20221211121251/https://www.geeksforgeeks.org/string-in-javascript/
Skip to content
Related Articles

Related Articles

String in JavaScript

Improve Article
Save Article
  • Last Updated : 09 Dec, 2022
Improve Article
Save Article

Strings are similar to sentences. They are made up of a list of characters, which is essentially just an “array of characters.” They come in quite handy when passing information from the programme to the user and storing textual data. In this article, we are going to talk about strings in Javascript.

Creating Strings: There are two ways to create a string in Javascript:

  • By string literal
  • By string object

Method 1: A string can be created just by initializing with a string literal. There are three ways to write a string literal – they can be written inside single quotes(‘ ‘), inside double quotes(” “), or inside backticks(` `). In the following code snippet, I am creating strings using all three ways of writing string literals.

// Using single quotes
const string1 = 'A string literal';

// Using double quotes
const string2 = "Another string literal";

// Using backticks
const string3 = `Yet another string literal`;

Method 2: A string can also be created using a String object. Below, I am using the string object to create the string.

// Using the String object
const string4 = new String("Using string object");

Character Access: A character can be accessed from a string in one of two ways. Using the charAt() method is the first one:

// Accessing character at third position
string1.charAt(2)

The alternative is to think of the string as an object resembling an array, where each character is represented by an integer index:

// Accessing character at third position
string1[2]

Let us go through some examples to better understand the usage of strings:

An array of Strings: To create an array of strings you can simply type:

// Creating an array of strings: string1, string2, string3
let arrString=["string1","string2","string3"];

or

// Creating an array of strings: string1, string2, string3
let arrString=new Array("string1","string2","string3");

Now let us go through some examples to understand strings better:

Example 1: In this example, we are going to concatenate two strings. To concatenate, I have simply used the + operator.

Javascript




<script>
    let a="Hello ";
    let b="World";
  
    // Concatenating strings a and b
    console.log(a+b);
</script>

Output:

Hello World

Example 2: In this example, I am going to find the index of the first occurrence of b in the string. To do that, I used the indexOf() method.

Javascript




<script>
    let a = "abcdefgh";
  
    // Finding the first index of the character 'b'
    console.log(a.indexOf('b'));
</script>

Output:

1

Example 3: In this example, I am trying to get the substring of the string. For that purposes, I have used the substring() method.

Javascript




<script>
    let a = "abcdefgh";
  
    // Printing the substring between the indices 2 and 7
    console.log(a.substring(2, 7));
</script>

Output:

cdefg

Example 4: In this example, I am replacing a part of the string with another string literal. I am using the replace() method to accomplish this.

Javascript




<script>
    let a = "Hello World";
  
    let arrString = ["Geeks", "for", "Geeks"]
  
    // Replacing the word 'World' with 'Geeks'
    console.log(a.replace("World", arrString[0]));
</script>

Output:

Hello Geeks

You can check out more string functions in javascript here.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!