Tutorial
Strings

String in JavaScript

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ What is String ?

Explain String With Examples

Imagine you're in your classroom at school, and your teacher asks you to write a story about your summer vacation. You decide to write about the time you went to the beach with your family.

To write your story, you'll need to use a lot of words. You'll need words like "beach", "vacation", "family", and so on. In JavaScript, each of these words is called a string.

A string is just a series of characters that represents some text. Just like you can use words to tell a story, you can use strings to write code.

For example, let's say you want to write a program that prints out the message "I had a great time at the beach!" to the screen. You could use a string to represent this message, like this:

'I had a great time at the beach!'

In this case, the string is just a bunch of characters (letters, numbers, and symbols) that are enclosed in quotes. The quotes tell JavaScript that everything inside them is a string.

You can use strings to do all sorts of things in JavaScript. For example, you can use them to display messages to the user, to store data, and to build up more complex programs.

⭐ Create JavaScript Strings

How to Create JavaScript Strings

  1. Single quotes: 'Hello'

  2. Double quotes: "Hello"

  3. Backticks: `Hello`

Example:

'This is a string.'
"This is also a string."
`This is also a string.`
😊
  • You can also use string concatenation to combine two or more strings together. To do this, you can use the + operator.

For example:

'Hello' + 'World'

This would produce the string "Hello, world!".

  • You can also use the += operator to concatenate strings together.

For example:

let greeting = 'Hello'
greeting += 'World'

This would also produce the string "Hello, world!".

  • You can also use \ operator to concatenate strings together.

For example:

const message2 = 'This is a long message \
that spans across multiple lines \
in the code.'

This would also produce the string "Hello, world!".

⭐ JavaScript Template Literals

Explain JavaScript Template Literals

In JavaScript, template literals are special strings that allow you to embed expressions inside them. They are written using backticks (`) instead of single or double quotes.

Here's an example of a template literal:

let person1 = 'Subham'
let person2 = 'Sourav'
console.log(`person2 is better than person1`)

This would produce the string "person2 is better than person1".

But what if you want to include the names of the two people in the string? You can do this by using the $ syntax inside the template literal.

For example:

let person1 = 'Subham'
let person2 = 'Sourav'
console.log(`${person1} is better than ${person2}`)

This would produce the string "Subham is better than Sourav".

⚡ Playground

ℹ️

You can also write a quote inside another quote. For example,

const name = 'My name is "Peter".'; // single quotes

However, the quote should not match the surrounding quotes. For example,

const name = 'My name is 'Peter'.'; // error

⭐ Access String Characters

How to Access String Characters ?

You can access individual characters in a string using square brackets ([]). The index of the first character in a string is 0, and the index of the last character is the length of the string minus 1.

let str = 'hello';
  1. To access the first character (the letter "h"), you would use the following code:
let firstChar = str[0];  // firstChar is "h"
  1. To access the last character (the letter "o"), you would use the following code:
let lastChar = str[str.length - 1];  // lastChar is "o"
  1. You can also use a loop to iterate over all of the characters in a string :
for (let i = 0; i < str.length; i++) {
    console.log(str[i]);
}
  1. Another way is to use the method charAt(). For example,
const a = 'hello';
console.log(a.charAt(1)); // "e"

⚡ Playground

⭐ Strings are Immutable

JavaScript Strings are immutable ?

In JavaScript, strings are immutable, which means that once you create a string, you cannot change its contents. If you want to modify a string, you'll need to create a new string that contains the modified version of the original string.

let str = 'hello';
str[0] = 'H';  // This will NOT change the string
ℹ️

To change the string, you'll need to create a new string that contains the modified version of the original string.

For example:

let str = 'hello';
let newStr = 'H' + str.slice(1);  // This creates a new string with the modified version of the original

Now the value of newStr is 'Hello', and the value of str is still 'hello'.

⚡ Playground