What is the String in JavaScript

JavaScript String global object is the constructor for strings or the sequence of characters. They can store and modify the text. Strings store and manipulate text data, such as words, sentences, or any combination of letters, numbers, and symbols.

Strings in JavaScript are immutable, which means that once they are created, their contents cannot be changed. Instead, any operation that appears to modify a string will create a new string with the modified content.

How to Create a string in JavaScript

There are two ways to create a string in JavaScript.

  1. Method 1: Using a string literal
  2. Method 2: Using a string object (using the new keyword)

Method 1: Using a string literal

The string literal is created using the double quotes. The syntax of creating the string using string literal is given below.

var str = 'appdividend'; 
const str = 'appdividend'; 
let str = 'appdividend';

You can use quotes inside the string if they don’t match the quotes surrounding a string.

let strA = "Hello World";
let strB = "He has called 'Krunal'";
let strC = 'He has called "Krunal"';

console.log(strA);
console.log(strB);
console.log(strC);

Output

By string literal

 

Method 2: Using a string object

The syntax for creating a string object using the new keyword is below.

let data = new String('AppDividend');

console.log(data);

Output

Javascript String Tutorial With Example

Finding a String length in JavaScript

To find a string length in JavaScript, use the built-in length property.

let strA = "Hello World";

console.log(strA.length);

Output

Find String Length in Javascript

Special Characters in JavaScript

let strA = "Hello World, I am "Krunal"";

Because strings must be written within quotes,  In the above code, JavaScript will misunderstand this string.

The string will be chopped to “Hello World, I am”.

The solution to avoid that problem is to use a backslash escape character.

The backslash (\) escape character turns the special characters into strings.

Code Result Description
\’ Single quote
\” Double quote
\\ \ Backslash

The sequence \”  inserts the double quote in a string.

Now, see the following code.

let strA = "Hello World, I am \"Krunal\", I am a Good Boii";

console.log(strA);

Output

Special Characters in Javascript

The sequence \’  inserts the single quote in a string.

let x = 'It\'s good.';

The sequence \\  inserts the backslash in a string.

let x = "The character \\ is called backslash.";

Six other escape characters are valid in JavaScript.

Code Result
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tabulator
\v Vertical Tabulator
Strings can be Objects

The JavaScript strings are primitive values created from the literal:

let nameA = "Ankit";

But the strings can also be defined as objects with the keyword new.

let nameB = new String("Ankit");

Let’s use a typeof operator to check the variables’ type.

let nameA = "Ankit"
let nameB = new String("Ankit")

console.log(typeof(nameA))
console.log(typeof(nameB))

Output

Strings Can be Objects
When using the == operator, equal strings are equal.
let nameA = "Ankit"
let nameB = new String("Ankit")

console.log(nameA == nameB)

Output

Double equal operator in Javascript

When using the === operator, equal strings are not equal because the === operator expects equality in both type and value.

Let’s check the above example with the === operator.

let nameA = "Ankit"
let nameB = new String("Ankit")

console.log(nameA === nameB)

Output

Triple equal operator in Javascript

String Properties in Javascript

Here is a list of the properties of String obj and their description.

Sr.No. Property & Description
1 constructor

Returns the reference to the String function that created the object.

2 length

Returns the length of the string.

3 prototype

The prototype property allows you to add properties and methods to an object.

JavaScript String Methods

Let’s see a list of JavaScript string methods.

Methods Description
charAt() It provides a char value present at the specified index.
charCodeAt() It provides a Unicode value of a character present at the specified index.
concat() It provides a combination of two or more strings.
indexOf() It provides a position of a char value present in the given string.
lastIndexOf() It provides a position of a char value in the given string by searching the character from the last position.
search() It searches the specified regular expression in a given string and returns its position if a match occurs.
match() It searches the specified regular expression in a given string and returns that regular expression if the match occurs.
replace() It replaces the given string with the specified replacement.
substring() It is used to fetch the part of the given string based on the specified index.
slice() It is used to fetch the part of the given string. It allows us to assign positive as well negative indexes.
toLowerCase() It converts a given string into a lowercase letter.
toLocaleLowerCase() It converts a given string into a lowercase letter based on the host’s current locale.
toUpperCase() It converts a given string into an uppercase letter.
toLocaleUpperCase() It converts a given string into an uppercase letter based on a host’s current locale.
toString() It provides a string representing a particular object.
valueOf() It provides the primitive value of the string object.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.