All about JavaScript  - String

All about JavaScript - String

Definition of String

"String is a sequence of characters used to form and represent a text. These can be alphabets, numbers, and special characters".

In this article, you will learn about:

  • String in JavaScript

  • How to create a String in JavaScript

  • String Datatype

  • Properties of String

  • String manipulation

  • String methods

  • Conclusion

Let's begin.

String in JavaScript

A String is a datatype in JavaScript, which creates a thread of characters to describe a text.

How to create a String in JavaScript

A variable is a named entity (myString) that reserves a space in the memory and is utilized to store values (string in double quotes) using these variables, defined using keywords such as var, const, and let.

// Declaring a variable and saving a string in the variable
let myString = "Harry Potter Series";

String Datatype

The string is a primitive type if created using string literal as below:

The string is made using double quotes (" "), single quotes (' '), and backticks (` `). These markings are used to assign values to a variable.

// Creating a string using double qoutes
let myString1 = "Harry Potter";

// Creating a string using single quotes
let myString2 = 'Ron Weasley';

// Creating a string using backtick or template liiterals
let myString3 = `Hermione Granger`;

String is an object type created using the String() constructor. A new keyword is used to create an object of String, and the value is passed with the brackets.

// Creating a new object of string and assigning it with a string 
let myStringObject = new String("Albus Dumbledore");

Properties of String in JavaScript

Properties are nothing but information about the string.

a). The length property returns the length of the string

let stringLength = myString.length;
console.log(stringLength);

b). The prototype property allows you to add properties and methods to existing objects.

function movie(name, genre, releaseDate){
  this.name = name;
  this.genre = genre;
  this.releaseDate = releaseDate;
}
//adding new property
movie.prototype.budget = "125 million USD";

//creating object of movie function using new keyword
const movieDetails = new movie("Harry Potter and the Sorcerer's Stone", Fantasy/Adventure, 2001);

//print the new property value using object
console.log(movieDetails.budget);

Output: 125 million USD

c). The constructor property returns the reference to the function that created the string object.

let stringMsg1 = "Hello World!";
let stringMsg2 = stringMsg1.constructor;
console.log(stringMsg2);

output: f String() { [native code] }

String Manipulation

String manipulation is a process of modifying strings to achieve a specific output. String concatenation, length, substring, replace, trim, etc., are a few ways to achieve the desired goal.

let str1 = "Harry";
let str2 = "Potter";
let str3 = "Series";

let fullName = str1.concat(' ', str2, ' ', str3);
console.log(fullName);

output: Harry Potter

let str1 = "Harry";
console.log(str1.length);

output: 5

String Method

String methods are built-in functions in the programming languages, here JavaScript, to obtain specific outcomes. Below are some commonly used string methods:

concat(string1, string2, string3, ...)

It concatenates two or more strings and results in one new string.

let str1 = "Harry";
let str2 = "Potter";
let str3 = "Series";

let fullName = str1.concat(' ', str2, ' ', str3);
console.log(fullName);

output: Harry Potter Series

charAt(index)

It returns the character at the specific index in the given string. The string index starts at 0.

let str1 = "Harry";

console.log(str1.charAt(1));

output: a

indexOf(value, atIndex)

It returns the start of the index of the given value.

let stringIndexOf = "Harry Potter"; 
let startIndex = stringIndexOf.indexOf("Potter"); 
console.log(startIndex);

output: 6

toUpperCase()

It returns the value all converted into upper-case.

let str1 = "harry";
console.log(str1.toUpperCase());

output: HARRY

toLowerCase()

It returns the value all converted into lower-case.


let str1 = "POTTER";
console.log(str1.toLowerCase());

output: potter

In conclusion, JavaScript strings are fundamental data types that represent sequences of characters, providing essential functionalities for manipulating and processing textual data within JavaScript applications. With versatile methods for concatenation, manipulation, and searching, JavaScript strings offer robust support for text handling, making them indispensable for web development and beyond.