loader

Loading

CoderrShyam logo

CoderrShyam

HomeAboutContactBlogTutorials
LoginSignup

Introduction

Quick StartWhat is JavaScriptHistory of JavaScript

Setup

Node.js InstallationVisual Studio Code

Basics

Syntax & Comments
Variables
Functions
JavaScript StringsJavaScripit ArraysLoops with ArraysMap, Filter, and ReduceDate in JavaScriptMath in JavaScriptNumber in JavaScriptBoolean in JavaScript

Advanced

CoderrShyam logo

CoderrShyam

HomeAboutContactBlogTutorials
LoginSignup
TutorialsJavaScript TutorialObjectsJavaScript Strings

JavaScript Strings

Strings are sequences of characters enclosed in single or double quotes. They are used to represent text in JavaScript.

One of the most important aspects of JavaScript is its ability to manipulate strings, which are sequences of characters. In this Tutorial, we will explore the basics of JavaScript strings and the various string methods that can be used to manipulate them.

Creating Strings

In JavaScript, strings are created by enclosing a sequence of characters in single or double quotes. Here are some examples of string literals:

main.js
let str1 = "Hello, World!";
let str2 = "JavaScript is awesome!";

You can also create strings using the String constructor:

let str3 = new String("Hello, World!");

However, it is more common to create strings using string literals as shown in the first examples.

String Methods

JavaScript provides a number of built-in methods that can be used to manipulate strings. Here are some common string methods:

length

The length property returns the length of a string:

main.js
let str = "Hello, World!";
console.log(str.length); // Output: 13

toUpperCase

The toUpperCase method converts a string to uppercase:

main.js
let str = "Hello, World!";
console.log(str.toUpperCase()); // Output: HELLO, WORLD!

toLowerCase

The toLowerCase method converts a string to lowercase:

main.js
let str = "Hello, World!";
console.log(str.toLowerCase()); // Output: hello, world!

charAt

The charAt method returns the character at a specified index in a string:

main.js
let str = "Hello, World!";
console.log(str.charAt(7)); // Output: W

indexOf

The indexOf method returns the index of the first occurrence of a specified substring in a string:

main.js
let str = "Hello, World!";
console.log(str.indexOf("World")); // Output: 7

concat

The concat method concatenates two or more strings:

main.js
let str1 = "Hello, ";
let str2 = "World!";
console.log(str1.concat(str2)); // Output: Hello, World!

split

The split method splits a string into an array of substrings based on a specified delimiter:

main.js
let str = "Hello, World!";
console.log(str.split(" ")); // Output: ["Hello,", "World!"]

slice

The slice method extracts a section of a string and returns it as a new string:

main.js
let str = "Hello, World!";
console.log(str.slice(7)); // Output: World!

replace

The replace method replaces a specified substring with another substring in a string:

main.js
let str = "Hello, World!";
console.log(str.replace("World", "JavaScript")); // Output: Hello, JavaScript!

trim

The trim method removes whitespace from both ends of a string:

main.js
let str = "   Hello, World!   ";
console.log(str.trim()); // Output: Hello, World!

startsWith

The startsWith method checks if a string starts with a specified substring:

main.js
let str = "Hello, World!";
console.log(str.startsWith("Hello")); // Output: true

endsWith

The endsWith method checks if a string ends with a specified substring:

main.js
let str = "Hello, World!";
console.log(str.endsWith("JavaScript")); // Output: false

includes

The includes method checks if a string contains a specified substring:

main.js
let str = "Hello, World!";
console.log(str.includes("World")); // Output: true

repeat

The repeat method returns a new string with a specified number of copies of the original string:

main.js
let str = "Hello, World!";
console.log(str.repeat(3)); // Output: Hello, World!Hello, World!Hello, World!

These are just a few examples of the many string methods available in JavaScript. You can find more information about string methods in the MDN Web Docs.

Conclusion

  • Strings are sequences of characters enclosed in single or double quotes.
  • JavaScript provides a number of built-in methods that can be used to manipulate strings.
  • Some common string methods include length, toUpperCase, toLowerCase, charAt, indexOf, concat, split, slice, replace, trim, startsWith, endsWith, includes, and repeat.

How is this guide?

February 26th, 2026

Functions

Functions are reusable blocks of code that perform a specific task. They allow you to break down your code into smaller, more manageable pieces.

JavaScripit Arrays

Arrays are used to store multiple values in a single variable. They are a special type of object that can hold multiple values at once.

© 2026CoderrShyamAll Rights Reserved.

On this page

Creating Strings
String Methods
length
toUpperCase
toLowerCase
charAt
indexOf
concat
split
slice
replace
trim
startsWith
endsWith
includes
repeat
Conclusion
Follow us on GitHub