Course
Strings
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Strings Object
The String object in JavaScript lets you work with a series of characters; it wraps JavaScript's string primitive data type with a number of helper methods.
As JavaScript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive.
The string is a sequence of characters containing 0 or more characters. For example, 'Hello' is a string.
Syntax
JavaScript strings can be created as objects using the
String()
constructor or as primitives using string literals.Use the following syntax to create a String object
const val = new String(value);
The String parameter, value is a series of characters that has been properly encoded.
We can create string primitives using string literals and the
String(
) function as follows −str1 = 'Hello World!'; // using single quotestr2 = "Hello World!"; // using double quotestr3 = 'Hello World'; // using back ticksstr4 = String('Hello World!'); // using String() function
String Properties
Here is a list of the properties of String object and their description.
String Methods
Here is a list of the methods available in String object along with their description.
Static Methods
The static methods are invoked using the 'String' class itself.
Instance Methods
The instance methods are invoked using the instance of the String class.
Example: Creating JavaScript String Objects
In the example below, we used the string() constructor with the 'new' keyword to create a string object.
<html><head> <title> JavaScript - String Object </title></head><body> <p id = "output"> </p> <script> const str = new String("Hello World!"); document.getElementById("output").innerHTML = "str == " + str + "<br>" + "typeof str == " + typeof str; </script></body></html>
Output
str == Hello World!typeof str == object
Accessing a String
You can access the string characters using its index. The string index starts from 0.
Example
In the example below, we access the character from the 0th and 4th index of the string.
<html><head> <title> JavaScript - Accessing a string </title></head><body> <p id = "output"> </p> <script> const str1 = new String("Welcome!"); let str2 = "Welcome!"; document.getElementById("output").innerHTML = "0th character is - " + str1[0] + "<br>" + "4th character is - " + str2[4]; </script></body></html>
Output
0th character is - W4th character is - o
JavaScript Strings are Case-sensitive
In JavaScript, strings are case-sensitive. It means lowercase and uppercase characters are different.
Example
In the example below, char1 contains the uppercase 'S', and char2 contains the lowercase 's' characters. When you compare char1 and char2, it returns false as strings are case-sensitive.
<html><head> <title> JavaScript - String case-sensitivity </title></head><body> <p id = "output"> <script> let char1 = 'S'; let char2 = 's' let ans = char1 == char2; document.getElementById("output").innerHTML += "s == S " + ans; </script></body></html>
Output
s == S false
JavaScript Strings are Immutable
In JavaScript, you can't change the characters of the string. However, you can update the whole string.
Example
In the example below, we try to update the first character of the string, but it doesn't get updated, which you can see in the output.
After that, we update the whole string, and you can observe the changes in the string.
<html><head> <title> JavaScript − Immutable String </title></head><body> <p id = "output"> </p> <script> const output = document.getElementById("output"); let str = "Animal"; str[0] = 'j'; output.innerHTML += "The string is: " + str + "<br>"; str = "Hi!"; output.innerHTML += "The updated string is: " + str; </script></body></html>
Output
The string is: AnimalThe updated string is: Hi!
Escape Characters
You can use the special characters with the string using the backslash (\) characters. Here is the list of special characters.
Example
In the example below, we added a single quote between the characters of the str1 string and a backslash between the characters of the str2 string.
<html><head> <title> JavaScript - Escape Characters </title></head><body> <p id = "output"> </p> <script> const output = document.getElementById("output"); let str1 = "Your\'s welcome!"; let str2 = "Backslash \\"; output.innerHTML += "str1 == " + str1 + "<br>"; output.innerHTML += "str2 == " + str2 + "<br>"; </script></body></html>
Output
str1 == Your's welcome!str2 == Backslash \
String HTML Wrappers
Here is a list of the methods that return a copy of the string wrapped inside an appropriate HTML tag.