1.String in Javascript
A string in JavaScript is a data type used to store text.
It is a sequence of characters such as letters, numbers, symbols, or spaces enclosed within quotes.
Example
`let name = "Hariharan";
let city = 'Chennai';
let message = "Hai";`
In the above examples
"Hariharan" is a string
"Chennai" is a string
"Hai" is a string
Important Points
Strings can be created using single quotes (' '), double quotes (" "), or backticks ( ).
Strings are used to store text values.
A string can contain letters, numbers, symbols, and spaces.
2.Why We Use Strings in JavaScript
We use strings in JavaScript to store and work with text data. Most applications need to handle text such as names, messages, addresses, or descriptions.
3.String Methods in JavaScript
In JavaScript, a string is a sequence of characters used to represent text. JavaScript provides many built-in string methods that allow developers to manipulate, analyze, and transform text data easily.
These methods help perform operations such as finding characters, extracting parts of a string, changing case, removing spaces, and replacing text.
One important thing to remember is that strings in JavaScript are immutable. This means string methods do not change the original string. Instead, they return a new string with the result.
1.Length
Returns the length of a string.
`let text = "Hari";
console.log(text.length);`
Output
10
2.charAt()
Returns the character at a specific index.
`let text = "Hariharan";
console.log(text.charAt(4));`
`Output
h`
3.charCodeAt()
Returns the Unicode value of a character.
`let text = "A";
console.log(text.charCodeAt(0));`
`Output
65`
What is Unicode
Unicode is a universal character encoding standard used to represent characters from almost all languages in the world.
It assigns a unique numeric code (number) to every character.
This allows computers to store and display text consistently across different systems and languages.
4.String Concat
The concat() method in JavaScript is used to join two or more strings together and return a new combined string.
It does not modify the original strings.
`let text1 = "Bumrah";
let text2 = "Bowls a Yorker";
let result = text1.concat(" ", text2);
console.log(result);`
`Output
Bumrah Bowls a Yorker`
5.toLowerCase()
Converts all characters to lowercase.
`let text = "HARIHARAN";
console.log(text.toLowerCase());`
`Output
hariharan`
6.toUppercase()
Converts all characters to uppercase
`let text = "hariharan";
console.log(text.toLowerCase());`
`Output
HARIHARAN`
6.Slice
Extracts a portion of a string
`let text = "JavaScript";
console.log(text.slice(0,4));`
`Output
Java`
7.trim()
Removes extra spaces from the beginning and end of a string
`let text = " Hello ";
console.log(text.trim());`
`Output
Hello`
String Search
JavaScript indexOf() Method
The indexOf() method in JavaScript is used to find the position (index) of the first occurrence of a specified value in a string.
If the specified value is not found, the method returns -1.
`let text = "Hello World";
console.log(text.indexOf("World"));`
`Output
6`
Important Points
indexOf() returns the first occurrence of a value.
If the value is not found, it returns -1.
It is case-sensitive.
Top comments (0)