Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

String in JavaScript

mac

String in JavaScript

Definition:

JavaScript String is a sequence of characters, typically used to represent text. Strings can be created using single quotes (` ' `), double quotes ( ` " `), or backticks (```). Here's a comprehensive overview of how strings work in JavaScript, including creation, manipulation.


key features:


1.Immutability:


  • Strings in JavaScript are immutable, meaning once a string is created, it cannot be changed. Operations on strings return new strings rather than modifying the original.


2.Template Literals:


  • Introduced in ES6, template literals provide an easy way to create multi-line strings and perform string interpolation using backticks (```).


3.Unicode Support:


  • JavaScript strings support Unicode, allowing for the representation of characters from virtually any written language.


4.Escape Sequences:


  • Special characters can be included in strings using escape sequences (e.g., `\n` for newline, `\t` for tab, and `\\ `for a backslash).


Key Methods of JavaScript Strings:


1. Character Access Methods:


char At(index):

Returns the character at the specified index.



char Code At(index):

Returns the Unicode of the character at the specified index.



`[ ]` notation:

Returns the Unicode of the character at the specified index.



2. String Search Methods:


index Of(substring, start):

Returns the index of the first occurrence of `substring`.




`last Index Of(substring, start)`: 

 Returns the index of the last occurrence of `substring`.




`includes(substring, start)`:

Checks if `substring` is found within the string.



3. String Extraction Methods:

`slice(start, end)`: 

 Extracts a part of the string and returns it as a new string.



`sub str(start, length)`:

Extracts a substring starting from `start` with the specified `length`.