JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Strings


📘 JavaScript Strings – Working with Text Data

A string in JavaScript is a sequence of characters enclosed in single ('), double ("), or backtick (``) quotes.

let name1 = "John";
let name2 = 'Doe';
let fullName = `${name1} ${name2}`;

🔹 How to Declare a String

let str1 = "Hello";
let str2 = 'World';
let str3 = `Hello World`;  // Template literal

🔹 Common String Properties and Methods

Method / Property Description Example
length Returns the length of the string "Hello".length → 5
charAt(index) Returns character at given index "Hello".charAt(1) → 'e'
toUpperCase() Converts to uppercase "hello".toUpperCase() → 'HELLO'
toLowerCase() Converts to lowercase "HELLO".toLowerCase() → 'hello'
indexOf(substr) Finds index of substring "hello".indexOf("l") → 2
lastIndexOf(substr) Last occurrence of substring "hello".lastIndexOf("l") → 3
includes(substr) Checks if substring exists "hello".includes("e") → true
startsWith(substr) Checks if string starts with value "hello".startsWith("he") → true
endsWith(substr) Checks if string ends with value "hello".endsWith("lo") → true
slice(start, end) Extracts part of string "hello".slice(1, 4) → 'ell'
substring(start, end) Similar to slice() "hello".substring(1, 4)
replace(a, b) Replaces first match "Hi there".replace("Hi", "Hello") → 'Hello there'
split(delimiter) Splits string into array "a,b,c".split(",") → ['a','b','c']
trim() Removes whitespace " hello ".trim() → 'hello'

🔹 Template Literals

let name = "John";
let message = `Hello, ${name}!`;  // Output: Hello, John!

Practice Questions

Q1. How do you find the length of the string "JavaScript"?

Q2. How do you convert the string "hello" to all uppercase letters using a built-in method?

Q3. How do you extract the substring "ava" from the string "JavaScript"?

Q4. How do you check whether a string "apple" includes the letter "p"?

Q5. How do you replace "good" with "awesome" in the string "good job"?

Q6. How do you convert "HELLO" to lowercase using a string method?

Q7. What method returns the position of the first occurrence of "a" in "banana"?

Q8. How do you split "red,green,blue" into an array of words?

Q9. How do you remove leading and trailing spaces from the string " hello "?

Q10. How do you create a greeting like "Hello, John!" using template literals with variable name = "John"?


JS Strings Quiz

Q1: Which method returns the number of characters in a string?

A. count()
B. length()
C. length
D. size()

Q2: What does "Apple".toLowerCase() return?

A. "APPLE"
B. "apple"
C. "Apple"
D. "AppLe"

Q3: Which of the following finds the first occurrence of "a" in "banana"?

A. indexOf("a")
B. find("a")
C. charAt("a")
D. slice("a")

Q4: Which method can convert "HELLO" to "hello"?

A. toLowerCase()
B. toUpperCase()
C. lower()
D. capitalize()

Q5: Which method checks if a string includes a specific word?

A. exists()
B. has()
C. includes()
D. in()

Q6: What is the result of "good job".replace("good", "awesome")?

A. "job awesome"
B. "good awesome"
C. "awesome job"
D. "awesomejob"

Q7: Which method removes white spaces from both sides of a string?

A. trim()
B. strip()
C. removeSpace()
D. clean()

Q8: What will "JavaScript".slice(4, 10) return?

A. "Java"
B. "Script"
C. "Scrip"
D. "cript"

Q9: Which of the following is used to insert variables inside strings using backticks?

A. '${variable}'
B. "${variable}"
C. `${variable}`
D. [variable]

Q10: Which of the following will split the string "one,two,three" into an array?

A. .explode(",")
B. .split(",")
C. .slice(",")
D. .toArray()

Go Back Top