In this article, we will learn about strings in JavaScript with examples.
JavaScript string is a primitive data type that is used to deal with text. Strings are used to store or manipulate text.
// Example of String const name = "Recursive Minds"
Ways to create strings in JavaScript
In JavaScript, strings are created by wrapping them with quotes. There are three ways you can create strings using quotes.
- Single quotes:
'Recursive Minds'
- Double quotes:
"Recursive Minds"
- Backticks:
`Recursive Minds`
For example:
// Example of String const name1 = 'Harry' // Using single quotes const name2 = "brook" // Using double quotes const friends = `${name1} and ${name2} are friends` // Using backticks
Single quotes and double quotes are the same you can use either of them.
Backticks are used when you want to use variables and expressions inside the string. This is done by surrounding variables and expressions with ${variable or expression}
as shown in the above example.
Access String Characters
There are two ways you can access characters from a string
- One is to treat the string as an array, For example
// Accessing character from string const a = 'Recursive Minds'; console.log(a[0]); // "R"
- Another way is to use the method
charAt()
, for example
// Accessing string character using charAt() method const a = 'Recursive Minds'; console.log(a.charAt(0)); // "R"
JavaScript Strings are Immutable
Strings in JavaScript are immutable, immutable means we cannot change the string character. For example
let a = 'Recursive Minds'; a[0] = 'A'; console.log(a); // "Recursive Minds"
You cannot change the character but you can assign a new string to the variable. For example
let a = 'recursive minds'; a = 'Recursive Minds'; console.log(a); // "Recursive Minds"
JavaScript Multiline Strings
In JavaScript, if you want to use multiline Strings, you can use +
and /
operators. For example
// using the + operator const message1 = 'Lorem ipsum dolor' + 'sit amet, consectetur' + 'adipiscing elit,'; // using the \ operator const message2 = 'Lorem ipsum dolor / sit amet, consectetur / adipiscing elit,';
JavaScript String Objects
In JavaScript, you can also create Strings using new
keyword. For example
// Example of String Object const str1 = 'Recursive Minds'; const str2 = new String('Recursive Minds'); console.log(str1); // "Recursive Minds" console.log(str2); // "Recursive Minds" console.log(typeof str1); // "string" console.log(typeof str2); // "object"
JavaScript String Length
In JavaScript, If you want to find the length of a String you can use the inbuilt length
property.
//Example of string length property const a = 'Recursive Minds'; console.log(a.length); // 15
JavaScript String()
Function
In JavaScript, String()
function is used to convert various data types to String. For example
const a = 555; // number const b = False; // boolean //converting to string using String() const result1 = String(a); const result2 = String(b); console.log(result1); // "555" console.log(result2); // "False"
Thank you so much for Reading
Happy Coding
For more details visit MDN Docs
Some other articles on JavaScript
- Introduction to JavaScript
- Variable in JavaScript
- JavaScript Data Types
- Let var const in JavaScript
- Operators in JavaScript
- JavaScript Conditionals
- for loop in JavaScript
- while and do-while in JavaScript
- break and continue in JavaScript