Edit Template
Edit Template

JavaScript Strings

In JavaScript, strings are sequences of characters used to represent text. Whether you’re showing a name, generating a message, or storing paragraphs, strings are everywhere.

You can define a string using either single quotes ('...') or double quotes ("..."). Both work the same:

				
					let msg1 = "Hello, World!";
let msg2 = 'Welcome to Learn With Arshyan!';

				
			

Both msg1 and msg2 are valid string values.

String Length

To check how many characters a string contains, use the .length property:

				
					let name = "Arshyan";
console.log(name.length); // 7

				
			

Joining Strings – concat()

You can join two or more strings together using the concat() method or the + operator:

				
					let first = "Learn";
let second = " With Arshyan";
let result = first.concat(second);
console.log(result); // Learn With Arshyan

				
			

Searching Inside a String – indexOf()

If you want to find where a specific word or letter appears in a string, use indexOf():

				
					let sentence = "Learning JavaScript is fun";
console.log(sentence.indexOf("JavaScript")); // 9

				
			

If the word doesn’t exist, it returns -1.

Extracting Parts – slice()

The slice() method is used to cut out a portion of a string:

				
					let title = "Frontend Development";
console.log(title.slice(0, 8)); // Frontend

				
			

It takes a starting index and an optional ending index.

Replacing Content – replace()

Need to update part of your string? Use replace():

				
					let msg = "Welcome, Guest!";
console.log(msg.replace("Guest", "Arshyan")); // Welcome, Arshyan!

				
			

This only replaces the first match unless you use a regular expression with a global flag.

Changing Case – toUpperCase() and toLowerCase()

These methods convert your string to uppercase or lowercase:

				
					let city = "Lahore";
console.log(city.toUpperCase()); // LAHORE
console.log(city.toLowerCase()); // lahore


				
			

Useful for formatting or comparisons.

Wrap-up

JavaScript strings are incredibly versatile. With methods like:

  • .length for size

  • .concat() to join

  • .indexOf() to search

  • .slice() to extract

  • .replace() to update

  • .toUpperCase() / .toLowerCase() to change case

…you have full control over text content.

Scroll to Top