Strings in JavaScript

A string is a text inside the quotes which can be either single or double quotes.
  1. var string1 = "Double Quoted String";  
  2. var string2 = 'Single Quoted String';
Concatenating the Strings : This can be achieved in javascript by using + operator or using concat() method.
 
Example using + operator:-
  1. var string1 = "Hello and Welcome";  
  2. var string2 = "to the Strings in JavaScript";  
  3. var result = string1 + " " + string2;  
  4. alert(result);  
Example using concat() method:-
  1. var string1 = "Hello and Welcome";  
  2. var string2 = "to the Strings in JavaScript";  
  3. var result = string1.concat(" ", string2);  
  4. alert(result);  
Output of the above both Examples : Hello and Welcome to the Strings in JavaScript
 
Using Single Quotes inside a String:-
 
Way 1 : If you are using double quotes for string so, use single quotes inside the string wherever needed.
  1. var str = "Hello and Welcome to the 'Strings' in JavaScript";  
  2. alert(str);  
Way 2 : If you are using single quotes for string so, use escape character followed by a single quotes inside the string wherever needed.
  1. var str = 'Hello and Welcome to the \'Strings\' in JavaScript';  
  2. alert(str);  
Output of both of the above codes : Hello and Welcome to the 'Strings' in JavaScript
 
Converting the Case of a String Value
 
Converting to Uppercase - This can be achieved by using toUpperCase() Function of JavaScript
  1. var str = "hello";  
  2. alert(str.toUpperCase());  
Output : HELLO
 
Code for Converting to Lowercase - This can be achieved by using toLowerCase() Function of JavaScript
  1. var str = "HELLO";  
  2. alert(str.toLowerCase());  
Output : hello
 
To find the Length of the String, use length() function of JavaScript
  1. alert("Hello".length);  
Output : 5
  1. var str = "Hello";  
  2. alert(str.length);  
Output : 5
 
trim() Function of JavaScript is used to remove extra spaces from the string.
  1. var str1 = " Hello ";  
  2. var str2 = " World ";  
  3. var result = str1.trim() + str2.trim();  
  4. alert(result);  
Output : HelloWorld