50 Beginner JavaScript String Questions with Answers
Q: How do you declare a string variable in JavaScript?
A: You can use let, const or var:
let str = 'Hello';
const str2 = "World";
var str3 = `Hello World`;
Q: What are the differences between single quotes, double quotes, and backticks for strings?
A: Single and double quotes are similar, but backticks (``) support template literals, allowing
interpolation like `${var}` and multi-line strings.
Q: How do you find the length of a string?
A: Use the .length property:
let str = 'Hello';
[Link]([Link]); // 5
Q: How do you access the first and last character of a string?
A: let str = 'Hello';
[Link](str[0]); // 'H'
[Link](str[[Link] - 1]); // 'o'
Q: How do you concatenate two strings?
A: Use + or concat():
let a = 'Hello';
let b = 'World';
[Link](a + ' ' + b);
[Link]([Link](' ', b));
Q: What is the difference between + operator and concat()?
A: + joins strings or string + number, concat() joins strings only.
Q: How do you convert a string to uppercase?
A: let str = 'hello';
[Link]([Link]()); // 'HELLO'
Q: How do you convert a string to lowercase?
A: let str = 'HELLO';
[Link]([Link]()); // 'hello'
Q: How do you check if a string contains a specific substring?
A: Use includes():
let str = 'Hello World';
[Link]([Link]('World')); // true
Q: How do you check if a string starts with a certain substring?
A: Use startsWith():
[Link]('Hello'.startsWith('He')); // true
Q: How do you check if a string ends with a certain substring?
A: Use endsWith():
[Link]('Hello'.endsWith('lo')); // true
Q: How do you extract a substring using substring()?
A: let str = 'Hello';
[Link]([Link](1,4)); // 'ell'
Q: How do you extract a substring using slice()?
A: let str = 'Hello';
[Link]([Link](1,4)); // 'ell'
Q: What is the difference between slice() and substring()?
A: slice() allows negative indexes; substring() swaps args if start > end.
Q: How do you extract a part of a string using substr()?
A: let str = 'Hello';
[Link]([Link](1,3)); // 'ell'
Q: How do you split a string into an array?
A: let str = 'a,b,c';
[Link]([Link](',')); // ['a','b','c']
Q: How do you trim whitespace from both ends of a string?
A: let str = ' hello ';
[Link]([Link]()); // 'hello'
Q: How do you trim only the start or end of a string?
A: [Link](), [Link]()
Q: How do you repeat a string multiple times?
A: let str = 'ha';
[Link]([Link](3)); // 'hahaha'
Q: How do you replace part of a string?
A: let str = 'Hello World';
[Link]([Link]('World','JS')); // 'Hello JS'
Q: What is the difference between replace() and replaceAll()?
A: replace() replaces first match; replaceAll() replaces all.
Q: How do you check if two strings are equal?
A: let a = 'test', b = 'test';
[Link](a === b); // true
Q: How do you compare strings in a case-insensitive manner?
A: [Link]() === [Link]()
Q: How do you find the index of a character in a string?
A: let str = 'Hello';
[Link]([Link]('l')); // 2
Q: How do you find the last occurrence of a character in a string?
A: [Link]('Hello'.lastIndexOf('l')); // 3
Q: How do you check if a string is empty?
A: [Link] === 0
Q: How do you reverse a string?
A: [Link]('').reverse().join('')
Q: How do you count occurrences of a character?
A: 'hello'.split('l').length - 1 // 2
Q: How do you convert a string to array of characters?
A: [Link]('')
Q: How do you check if a string contains only digits?
A: /^\d+$/.test(str)
Q: How do you check if a string contains only letters?
A: /^[a-zA-Z]+$/.test(str)
Q: How do you capitalize first letter?
A: str[0].toUpperCase() + [Link](1)
Q: How do you pad a string to length?
A: '5'.padStart(3,'0') // '005'
Q: How do you remove all spaces?
A: [Link](/\s/g,'')
Q: How do you insert at a position?
A: [Link](0,pos)+insert+[Link](pos)
Q: How do you check palindrome?
A: str === [Link]('').reverse().join('')
Q: How do you escape special characters?
A: Use \\, \", \' etc.
Q: How do you get Unicode of a char?
A: 'A'.charCodeAt(0) // 65
Q: How do you convert Unicode to char?
A: [Link](65) // 'A'
Q: How do you split a string into words?
A: [Link](' ')
Q: How do you join array into string?
A: [Link](' ')
Q: How do you remove duplicate characters?
A: [...new Set(str)].join('')
Q: How do you find vowels?
A: [Link](/[aeiou]/gi)
Q: How do you count words?
A: [Link]().split(/\s+/).length
Q: How do you format string with template literals?
A: `Hello ${name}`
Q: How do you access char using charAt()?
A: [Link](0)
Q: How do you access char using array indexing?
A: str[0]
Q: How do you convert string to number?
A: parseInt(str) or Number(str)
Q: How do you handle multi-line strings?
A: Use backticks (``)