LearnFrenzy
Sign in Take a free test
JS

JavaScript Programming · Topic 10 of 25

Strings Questions and Concepts

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Strings · Study material

JavaScript Strings – Interview Questions

1. Are strings mutable in JavaScript?

No. String values never change; methods such as toUpperCase and replace return new strings. Assigning to str[0] has no effect (TypeError in strict mode).

2. What is the difference between single quotes, double quotes and backticks?

Single and double quotes are equivalent. Backticks create template literals, which support ${} interpolation, multi-line text and tags.

3. Why is "😀".length 2?

length counts UTF-16 code units, and characters outside the Basic Multilingual Plane are stored as two units (a surrogate pair). [..."😀"].length is 1.

4. What is the difference between slice and substring?

slice supports negative indexes counted from the end. substring treats negative values as 0 and swaps the arguments when start is greater than end.

5. What is the difference between charAt(i) and str[i]?

Out of range, charAt returns "" while bracket access returns undefined. at(i) also accepts negative indexes.

6. How are strings compared with < and >?

Code unit by code unit (lexicographically by UTF-16 value), so "Z" < "a" and "10" < "9". Use localeCompare for natural ordering.

7. What does localeCompare return?

A negative number, 0 or a positive number depending on whether the string comes before, equals or after the argument in the given locale; options can ignore case or accents.

8. What is the difference between String(x) and x.toString()?

String(x) works for every value including null and undefined; x.toString() throws for null and undefined.

9. What are tagged templates?

A function placed before a template literal receives the array of literal parts and the evaluated values, and can return anything – used for escaping, i18n, styled-components and String.raw.

10. What does String.raw do?

Returns the raw text of a template literal, with escape sequences such as \n left uninterpreted (placeholders are still filled in).

11. Why should you avoid new String("x")?

It creates a wrapper object: typeof is "object", === to the primitive is false, and it is always truthy – even new String("").

12. How do you reverse a string?

[...str].reverse().join(""); strings have no reverse method, and split("") breaks surrogate pairs.

13. How do you check whether a string contains a substring?

includes (boolean), indexOf (position or -1), startsWith/endsWith for the ends – all case-sensitive.

14. How do you convert a string to a number?

Number(str) or unary + for whole-string conversion, parseInt(str, 10)/parseFloat(str) to read a numeric prefix.

15. How do you make a case-insensitive comparison?

Compare a.toLowerCase() === b.toLowerCase(), or use localeCompare with { sensitivity: "base" } (which also ignores accents).

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About JavaScript Programming

Why it matters

  • JavaScript runs every web page and, with Node.js, servers, tools and APIs – it is the most used language in the world
  • Front-end, full-stack and MERN roles test JavaScript first, and many campus coding rounds allow it
  • "Predict the output" questions on hoisting, closures, this, coercion and the event loop are interview favourites
  • Modern ES6+ – let/const, arrow functions, destructuring, promises and async/await – is what companies expect from freshers

Who it is for

  • College students (B.Tech, BCA, MCA, B.Sc CS) learning web development and preparing for semester exams and viva
  • Freshers preparing for campus placements, online assessments and front-end interviews
  • Developers moving to React, Angular, Vue or Node.js who need strong core JavaScript
  • Anyone revising ES6 and asynchronous JavaScript before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs on concepts and ES6 features
  • "Predict the output" puzzles – hoisting, coercion, closures, promises and timers
  • "Find the error" debugging: SyntaxError, ReferenceError, TypeError and silent logic bugs
  • Coding problems with solutions and real output, every one run on Node.js – run any study example in your browser

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture, Cognizant tests
  • Front-end developer interviews
  • MERN / full-stack interviews
  • Online coding assessments
  • University practicals

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, debugging, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links