MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Common String Methods0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
6.1String Basics6.2Common String Methods6.3Template Literals (Basics)
HomeTutorialsJavaScript for BeginnersCommon String Methods
Template Literals (Basics)

Common String Methods

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

JavaScript provides a rich library of built-in methods on strings to search, extract, replace, format, and split text. Because strings are immutable, all string methods return a new string without modifying the original.


1. Case Transformation & Trimming

1const rawInput = " sumit sharma ";
2 
3// 1. Convert to Uppercase
4console.log(rawInput.toUpperCase()); // " SUMIT SHARMA "
5 
6// 2. Convert to Lowercase
7console.log(rawInput.toLowerCase()); // " sumit sharma "
8 
9// 3. Trim whitespace from both ends (Essential for form inputs!)
10const cleanInput = rawInput.trim();
11console.log(cleanInput); // "sumit sharma"
12 
13// Trim only start or only end:
14console.log(rawInput.trimStart());
15console.log(rawInput.trimEnd());

2. Searching in Strings

A. .includes() (ES6)

Checks if a substring exists; returns true or false (case-sensitive):

1const email = "student@mskinstitute.com";
2console.log(email.includes("@")); // true
3console.log(email.includes("yahoo")); // false

B. .startsWith() and .endsWith() (ES6)

1const filename = "report_q3_final.pdf";
2console.log(filename.startsWith("report")); // true
3console.log(filename.endsWith(".pdf")); // true

C. .indexOf() and .lastIndexOf()

Returns the starting index of substring, or -1 if not found:

1const phrase = "Learn JavaScript at MSK";
2console.log(phrase.indexOf("JavaScript")); // 6
3console.log(phrase.indexOf("Python")); // -1

3. Extracting Substrings: .slice()

The .slice(startIndex, endIndex) method extracts a portion of a string:

  • startIndex: Index where extraction begins (inclusive).
  • endIndex (optional): Index where extraction ends (exclusive).
  • Supports negative indices to count backward from the end!
1const text = "JavaScript";
2 
3console.log(text.slice(0, 4)); // "Java" (indices 0, 1, 2, 3)
4console.log(text.slice(4)); // "Script" (from index 4 to the end)
5console.log(text.slice(-6)); // "Script" (last 6 characters)

4. Replacing Text: .replace() and .replaceAll()

1const message = "JavaScript is slow. JavaScript is hard.";
2 
3// .replace() only replaces the FIRST occurrence:
4console.log(message.replace("JavaScript", "Python"));
5// Output: "Python is slow. JavaScript is hard."
6 
7// .replaceAll() (ES2021) replaces ALL occurrences:
8console.log(message.replaceAll("JavaScript", "JS"));
9// Output: "JS is slow. JS is hard."

5. Splitting Strings into Arrays: .split()

.split(delimiter) splits a string into an array of substrings based on a separator:

1// Split by comma:
2const tags = "react,javascript,frontend,css";
3const tagList = tags.split(",");
4console.log(tagList); // ["react", "javascript", "frontend", "css"]
5 
6// Split by space (words):
7const sentence = "Welcome to MSK Institute";
8const words = sentence.split(" ");
9console.log(words); // ["Welcome", "to", "MSK", "Institute"]
10 
11// Split into individual characters:
12const letters = "MSK".split("");
13console.log(letters); // ["M", "S", "K"]

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does str.trim() do?
Question 2
What is returned by "Frontend".slice(0, 5)?
Question 3
If str = "apple,banana,orange", what does str.split(",") return?
Question 4
What does "coding".includes("od") evaluate to?
Question 5
What is the output of "hello".indexOf("z")?
Next Lesson

Template Literals (Basics)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
String BasicsTemplate Literals (Basics)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext