Strings in JavaScript with examples

Today, we are going to learn about strings in JavaScript.

What are the Strings?

Strings are used for storing character-based data which we can manipulate also.
Basically, it is the collection of characters that may be zero or more than zero characters also.

Mainly, there are 3 ways to assign strings in JavaScript.

let str1="CodeSpeedy";   // Using Double quotes
let str2='Hello CodeSpeedy';   // Using Single quotes
let str3=`Hello world`; //Using BackTick below Esc key

Why there are more than one ways to assign a string?

The reason is, what if you want to show ” ” or ‘ ‘ in your string. You can use single quotes to show double quotes and vise versa.

let str='This is "CodeSpeedy"';
console.log(str);
//suppose if you assign str="This is "CodeSpeedy"" then it will consider two string separately and gives error.

Using 3rd way you can show variables within a string.

let a="Hello";
let b='how are you?';
let age=21;
console.log(a,b); //String concatenation
console.log(`${a} ${b} I'm ${age} years old`);

JavaScript String Methods and Properties:

-> String Length

Here, length is the property and the return length of the string.

let s="Anil Sharma";
console.log(s.length); // length is 11 (including space)

-> Extracting string from the given string

1. slice(start,end) method

let s="Anil Sharma";
console.log(s.slice(2,5)); 
// output is il
// slice is also taking negative value
// negative value starts from right with 1 not 0
console.log(s.slice(-5,9));

2. substring(start,end) method

let s="Hello world";
console.log(s.substring(2,5)); 
// output is llo
//indexing starts from 0 to given index

3. substr(start,length) method

let s="Hello world";
console.log(s.substr(2,5)); 
// output is llo w
//indexing starts from 0 to length(no. of character)

-> String replace(str1,str2) method

let str= "This is the animal";
let ans = str.replace("animal", "bird");
console.log(ans); //output: This is the bird

-> String concat() method
It will concatenate(join) the two strings.

let str1= "Hello ";
let str2= "World";
// first way of string concatenation
let ans = str1.concat(str2); 
console.log(ans); //output: Hello World
// second way of string concatenation
ans=str1+"CodeSpeedy";
console.log(ans); //output: Hello CodeSpeedy

-> String trim() method
It will remove the extra spaces.

let str1= "      Hello   ";
console.log(str1); //output:      Hello   
console.log(str1.trim());  //output:Hello

-> String toUpperCase() and toLowerCase() methods
toUpperCase() will convert all characters to uppercase.
toLowerCase() will convert all characters to lower case.

let str1= "Hello";
console.log(str1.toUpperCase());   //output:HELLO   
console.log(str1.toLowerCase());  //output:hello

-> String charAt() and charCodeAt()
str.charAt(index) will gives the value of respected index.
str.charCodeAt() will gives the ASCII value of respected index.

let str1= "Hello";
console.log(str1.charAt(1));   //output:e  
console.log(str1.charCodeAt(1));  //output:101

-> String split()
str.split will split the string into array.

let str1= "Hello";
let arr=[];
arr=str1.split("");
console.log(arr);   //output:[ 'H', 'e', 'l', 'l', 'o' ]

There are many methods in the string but these are the most common methods.
write a comment on which topic do you want next blog.

 

Leave a Reply

Your email address will not be published. Required fields are marked *