J

JavaScript Handbook

Clean • Professional

JavaScript Strings — Top Interview Questions & Answers

3 minute

JavaScript Strings — Interview Questions & Answers

Ques: What is a String in JavaScript?

Ans: A string is a sequence of characters used to represent text in JavaScript.

Strings are enclosed in quotes — single (' '), double (" "), or backticks ( ).

Example:

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

Ques: What are the different ways to create a string in JavaScript?

MethodExampleDescription
String literallet a = "Hello";Most common and simple way
String objectlet b = new String("Hello");Creates a string object (not recommended)

Example:

let str1 = "ChatGPT";
let str2 = new String("ChatGPT");
console.log(typeof str1); // "string"
console.log(typeof str2); // "object"

Ques: How can you find the length of a string?

Ans: Use the .length property.

let text = "JavaScript";
console.log(text.length); // 10

Ques: How can you access individual characters in a string?

Ans: Using index notation or .charAt() method.

let text = "Hello";
console.log(text[0]);      // "H"
console.log(text.charAt(1)); // "e"

Ques: What is Template Literal in JavaScript?

Ans: Template literals (backticks ) allow embedding variables and expressions directly inside strings.

let name = "Alice";
let greet = `Hello, ${name}!`;
console.log(greet); // "Hello, Alice!"

They also support multi-line strings:

let msg = `
This is
a multi-line
string.
`;

Ques: What is the difference between slice(), substring(), and substr()?

MethodParametersNegative IndexExampleOutput
slice(start, end)Start, endYes"JavaScript".slice(0,4)"Java"
substring(start, end)Start, endNo"JavaScript".substring(4,10)"Script"
substr(start, length)Start, lengthYes"JavaScript".substr(4,6)"Script"

Ques: How can you search within a string?

Ans: Use these methods:

"JavaScript".includes("Script"); // true
"JavaScript".startsWith("Java"); // true
"JavaScript".endsWith("Script"); // true

Ques: How can you replace all occurrences of a word in a string?

Ans: Use regular expressions with g flag.

let text = "I like JS and JS";
console.log(text.replace(/JS/g, "JavaScript"));
// Output: "I like JavaScript and JavaScript"

Ques: What is the difference between == and === when comparing strings?

  • == compares values only
  • === compares values and types
"10" == 10;  // true
"10" === 10; // false

Ques: How to convert a number to a string?

String(100);       // "100"
(100).toString();  // "100"

Ques: How to convert a string to a number?

Number("50");   // 50
parseInt("50"); // 50
parseFloat("50.5"); // 50.5

Ques: How to check if a value is a string?

typeof "hello" === "string"; // true
"hello" instanceof String;   // false
new String("hello") instanceof String; // true

Ques: What are escape characters in strings?

Ans: Special characters preceded by a backslash (\\).

EscapeMeaningExample
\\'Single quote'It\\'s fine'
\\"Double quote"He said \\"Hello\\""
\\\\Backslash"C:\\\\User\\\\Docs"
\\nNew line"Hello\\nWorld"
\\tTab space"Hello\\tWorld"

Ques: Can strings be modified after creation?

Ans: No, Strings in JavaScript are immutable. Any modification creates a new string.

let str = "Hello";
str[0] = "Y";
console.log(str); // "Hello" (unchanged)

Ques: How can you reverse a string in JavaScript?

Ans: Convert it to an array → reverse → join back:

let str = "hello";
let rev = str.split("").reverse().join("");
console.log(rev); // "olleh"

Ques: How to count occurrences of a character in a string?

let text = "banana";
let count = text.split("a").length - 1;
console.log(count); // 3

 

Article 0 of 0