- Using single quotes.
- Using double quotes.
- Using backticks.
Single quotes and double quotes are the same so you can use either of them.
Access string characters
- There are two ways to available to access characters from the sting.
let name = "Programming"; console.log(name); // r
- And another ways to access character from the string using string charAt() function.
let name = "Programming"; console.log(charAt(2)); // o
let name = "Programming"; name = 'a'; console.log(name);
To use multi line string , you can use + operator or slash \.
let name = "Programming Funda " + "is the best place to learn coding."; console.log(name); // Output // Programming Funda is the best place to learn coding.
let name = 'Programming Funda \ is the best place to learn coding.'; console.log(name); // Output // Programming Funda is the best place to learn coding.
To find the length of the string use built-in length property.
let name = "Programming Funda" console.log(name.length);
Sometimes we want to write a special character within a string.
for example, we want to display a message like: Welcome to “programming funda tutorial”.
var name = "Welcome to "programming funda tutorial"" console.log(name);
To avoid the above problem we will use the backlash ( \ ) escape character. The backslash ( \ ) escape character turns special characters into string characters.
var name = "Welcome to \"programming funda tutorial\"" console.log(name);
var name = "Welcome to \"programming funda tutorial\""; console.log(name);
To check the type of string you can use typeof operator.
let name1 = "Programming Funda"; let name2 = new String("Programming Funda"); console.log(typeof name1); console.log(typeof name2); //Output // string // object
When we will use ( == ) operator it will return true because both a and b has same value.
var a = new String("programming Funda"); var b = "programming Funda"; console.log(a == b);
When we will use ( === ) operator, it will return false because both a and b has same value but different data type.
var a = new String("programming Funda"); var b = "programming Funda"; console.log(a === b);
- charAt():- return the character at the specified position.
- concat():- Join two or more strings.
- replace():- replace a string with another string.
- split():- Convert the string into an array of the string.
- substr(start, length):- return the part of the string with a specified length.
- substring(start end):- Return a part of the string.
- slice():- Return a part of string.
- toLowerCase():- Return a passed string into lower case.
- toUpperCase():- Return a passed string into upper case.
- trim():- Remove whitespaces from the string.
- includes():- Searches for a string and returns a boolean value.
- search():- Searches for a string and returns the position of matches.
let name1 = " Programming "; let name2 = "Funda"; console.log(name2.charAt(1)); // u console.log(name1.concate(name2)); // // Convert text to upper case console.log(name2.toUpperCase()); // Convert text to lower case console.log(name2.toLowerCase()); // Remove whitespace from the string. console.log(name1.trim());
I hope this article will have helped you. If you like this guide, don’t forget to share.