0% found this document useful (0 votes)
1 views5 pages

JavaScript String Questions Answers

The document contains 50 beginner-level questions and answers about JavaScript strings. It covers various string operations such as declaration, manipulation, and methods for checking and formatting strings. Each question is answered concisely, providing essential information for understanding string handling in JavaScript.

Uploaded by

Mohd Shamail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

JavaScript String Questions Answers

The document contains 50 beginner-level questions and answers about JavaScript strings. It covers various string operations such as declaration, manipulation, and methods for checking and formatting strings. Each question is answered concisely, providing essential information for understanding string handling in JavaScript.

Uploaded by

Mohd Shamail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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';
console.log(str.length); // 5

Q: How do you access the first and last character of a string?


A: let str = 'Hello';
console.log(str[0]); // 'H'
console.log(str[str.length - 1]); // 'o'

Q: How do you concatenate two strings?


A: Use + or concat():
let a = 'Hello';
let b = 'World';
console.log(a + ' ' + b);
console.log(a.concat(' ', 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';
console.log(str.toUpperCase()); // 'HELLO'

Q: How do you convert a string to lowercase?


A: let str = 'HELLO';
console.log(str.toLowerCase()); // 'hello'

Q: How do you check if a string contains a specific substring?


A: Use includes():
let str = 'Hello World';
console.log(str.includes('World')); // true

Q: How do you check if a string starts with a certain substring?


A: Use startsWith():
console.log('Hello'.startsWith('He')); // true

Q: How do you check if a string ends with a certain substring?


A: Use endsWith():
console.log('Hello'.endsWith('lo')); // true

Q: How do you extract a substring using substring()?


A: let str = 'Hello';
console.log(str.substring(1,4)); // 'ell'

Q: How do you extract a substring using slice()?


A: let str = 'Hello';
console.log(str.slice(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';
console.log(str.substr(1,3)); // 'ell'

Q: How do you split a string into an array?


A: let str = 'a,b,c';
console.log(str.split(',')); // ['a','b','c']

Q: How do you trim whitespace from both ends of a string?


A: let str = ' hello ';
console.log(str.trim()); // 'hello'

Q: How do you trim only the start or end of a string?


A: str.trimStart(), str.trimEnd()

Q: How do you repeat a string multiple times?


A: let str = 'ha';
console.log(str.repeat(3)); // 'hahaha'

Q: How do you replace part of a string?


A: let str = 'Hello World';
console.log(str.replace('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';
console.log(a === b); // true

Q: How do you compare strings in a case-insensitive manner?


A: a.toLowerCase() === b.toLowerCase()

Q: How do you find the index of a character in a string?


A: let str = 'Hello';
console.log(str.indexOf('l')); // 2

Q: How do you find the last occurrence of a character in a string?


A: console.log('Hello'.lastIndexOf('l')); // 3

Q: How do you check if a string is empty?


A: str.length === 0

Q: How do you reverse a string?


A: str.split('').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: str.split('')

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() + str.slice(1)

Q: How do you pad a string to length?


A: '5'.padStart(3,'0') // '005'

Q: How do you remove all spaces?


A: str.replace(/\s/g,'')

Q: How do you insert at a position?


A: str.slice(0,pos)+insert+str.slice(pos)

Q: How do you check palindrome?


A: str === str.split('').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: String.fromCharCode(65) // 'A'

Q: How do you split a string into words?


A: str.split(' ')

Q: How do you join array into string?


A: arr.join(' ')

Q: How do you remove duplicate characters?


A: [...new Set(str)].join('')

Q: How do you find vowels?


A: str.match(/[aeiou]/gi)

Q: How do you count words?


A: str.trim().split(/\s+/).length

Q: How do you format string with template literals?


A: `Hello ${name}`

Q: How do you access char using charAt()?


A: str.charAt(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 (``)

You might also like