In this tutorial, you will learn about JavaScript string with the assistance of examples.
JavaScript strings are used for storing and manipulating text.
The string is a primitive data type in JavaScript. A string is a textual substance. It should be enclosed in single or twofold quotes.
JavaScript string is a primitive data type that is used to work with texts. For instance,
const name = 'Salman';
In this article, you will learn-
- 1 Create JavaScript Strings
- 2 Access String Characters
- 3 JavaScript Strings are immutable
- 4 JavaScript is Case-Sensitive
- 5 JavaScript Multiline Strings
- 6 JavaScript String Length
- 7 JavaScript String Objects
- 8 JavaScript String Methods
- 9 Example: JavaScript String Methods
- 10 JavaScript String() Function
- 11 Escape Character
Create JavaScript Strings
In JavaScript, strings are made by encompassing them with quotes. There are three different ways you can use quotes.
- Single quotes: ‘Hello’
- Double quotes: “Hello”
- Backticks:
Hello
For example,
//strings example const name = 'Sohail'; const name1 = "Salman"; const result = `The names are ${name} and ${name1}`;
Single quotes and twofold quotes are essentially the equivalent and you can use both of them.
Backticks are by and large used when you need to incorporate variables or articulations into a string. This is finished by wrapping variables or articulations with ${variable or expression} as appeared above.
You can likewise write a quote inside another quote. For instance,
const name = 'My name is "Sohail".';
However, the quote should not match the surrounding quotes. For example,
const name = 'My name is 'Sohail'.'; // error
Access String Characters
You can get to the characters in a string twoly.
- One path is to treat strings as an array. For instance,
const a = 'hello'; console.log(a[1]); // "e"
- Another way is to use the method charAt(). For example,
const a = 'hello'; console.log(a.charAt(1)); // "e"
JavaScript Strings are immutable
In JavaScript, strings are immutable. That implies the characters of a string can’t be changed. For instance,
let a = 'hello'; a[0] = 'H'; console.log(a); // "hello"
However, you can assign the variable name to a new string. For example,
let a = 'hello'; a = 'Hello'; console.log(a); // "Hello"
JavaScript is Case-Sensitive
JavaScript is case-sensitive. That implies in JavaScript, the lowercase and uppercase letters are treated as various qualities. For instance,
const a = 'a'; const b = 'A' console.log(a === b); // false
In JavaScript, a and A are treated as different values.
JavaScript Multiline Strings
To use a multiline string, you can either use the + operator or the \ operator. For instance,
// using the + operator const message1 = 'This is a long message ' + 'that spans across multiple lines' + 'in the code.' // using the \ operator const message2 = 'This is a long message \ that spans across multiple lines \ in the code.'
JavaScript String Length
To find the length of a string, you can use the built-in length property. For example,
const a = 'hello'; console.log(a.length); // 5
JavaScript String Objects
You can also create strings using the new keyword. For example,
const a = 'hello'; const b = new String('hello'); console.log(a); // "hello" console.log(b); // "hello" console.log(typeof a); // "string" console.log(typeof b); // "object"
Note: It is prescribed to abstain from using string objects. Using string objects showdown the program.
JavaScript String Methods
Here are the normally utilized JavaScript String strategies:
Method | Description |
charAt(index) | returns the character at the specified index |
concat() | joins two or more strings |
replace() | replaces a string with another string |
split() | converts the string to an array of strings |
substr(start, length) | returns a part of a string |
substring(start,end) | returns a part of a string |
slice(start, end) | returns a part of a string |
toLowerCase() | returns the passed string in lower case |
toUpperCase() | returns the passed string in upper case |
trim() | removes whitespace from the strings |
includes() | searches for a string and returns a boolean value |
search() | searches for a string and returns a position of a match |
Example: JavaScript String Methods
const text1 = 'hello'; const text2 = 'world'; const text3 = ' JavaScript '; // concatenating two strings const result1 = text1.concat(' ', text2); console.log(result1); // "hello world" // converting the text to uppercase const result2 = text1.toUpperCase(); console.log(result2); // HELLO // removing whitespace from the string const result3 = text3.trim(); console.log(result3); // JavaScript // converting the string to an array const result4 = text1.split(); console.log(result4); // ["hello"] // slicing the string const result5= text1.slice(1, 3); console.log(result5); // "el"
JavaScript String() Function
The String() work is used to change over different data types to strings. For instance,
const a = 225; // number const b = true; // boolean //converting to string const result1 = String(a); const result2 = String(b); console.log(result1); // "225" console.log(result2); // "true"
If you want to learn more about string conversion, visit JavaScript Type Conversion.
Escape Character
You can use the backslash escape character \ to include special characters in a string. For example,
const name = 'My name is \'Sohail\'.'; console.log(name);
Output
My name is 'Sohail'.
In the above program, a similar quote is incorporated using .
Here are alternate ways that you can use \:
Code | Output |
\” | include double quote |
\\ | include backslash |
\n | new line |
\r | carriage return |
\v | vertical tab |
\t | horizontal tab |
\b | backspace |
\f | form feed |
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.