A string is a text inside the quotes which can be either single or double quotes.
- var string1 = "Double Quoted String";
- var string2 = 'Single Quoted String';
Concatenating the Strings : This can be achieved in javascript by using + operator or using concat() method.
Example using + operator:-
- var string1 = "Hello and Welcome";
- var string2 = "to the Strings in JavaScript";
- var result = string1 + " " + string2;
- alert(result);
Example using
concat() method:-
- var string1 = "Hello and Welcome";
- var string2 = "to the Strings in JavaScript";
- var result = string1.concat(" ", string2);
- 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.
- var str = "Hello and Welcome to the 'Strings' in JavaScript";
- 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.
- var str = 'Hello and Welcome to the \'Strings\' in JavaScript';
- 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
- var str = "hello";
- alert(str.toUpperCase());
Output : HELLO
Code for Converting to Lowercase - This can be achieved by using toLowerCase() Function of JavaScript
- var str = "HELLO";
- alert(str.toLowerCase());
Output : hello
To find the Length of the String, use length() function of JavaScript
- var str = "Hello";
- alert(str.length);
Output : 5
trim() Function of JavaScript is used to remove extra spaces from the string.
- var str1 = " Hello ";
- var str2 = " World ";
- var result = str1.trim() + str2.trim();
- alert(result);
Output : HelloWorld