Skip to content

Latest commit

 

History

History
236 lines (178 loc) · 5.34 KB

7.Strings.md

File metadata and controls

236 lines (178 loc) · 5.34 KB

100 JS Questions (Strings)

Q1. What is a String?

A string is a data type used to store and manipulate data.

//Single quotes ('')
var str1 = 'Hello';

Q2. What are template literals and string interpolation in strings? V. IMP.

A template literals, also known as a template string, is a feature introduced in ECMAScript 2015 (ES6) for string interpolation and multiline strings in JavaScript.

${Template} Literals

// Backticks (`)
// Template literals with string interpolation
var myname = "Happy";
var str3 = `Hello ${myname}!`;
console.log(str3);
// Output: Hello Happy!

// Backticks (`)
// Template literals for multiline strings
var multilineStr = `
This is a
multiline string.
`;

Q3. What is the difference between single quotes ('), double quotes (") & backticks (`)?

// Single quotes ('')
var str1 = 'Hello';

// Double quotes ("")
var str2 = "World";
// Backticks (`)
//Template literals with string interpolation
var myname = "Happy";
var str3 = `Hello ${myname}!`;
console.log(str3);
// Output: Hello Happy!

// Backticks (`)
//Template literals for multiline strings
var multilineStr = `
This is a
multiline string.
`;

Q4. What are some important string operations in JS?

  • substr()

  • indexOf()

  • trim()

  • substring()

  • includes()

  • charAt()

  • replace()

  • slice()

  • valueOf()

  • search()

  • concat()

  • split()

  • toLocaleLowerCase()

  • lastIndexOf()

  • toString()

  • toLocaleUpperCase()

  • charCodeAt()

  • match()

  • Accessing Characters

    charAt(index): Returns the character at a specified index.

    let str = "Hello, world!";
    console.log(str.charAt(0)); // Output: H

    charCodeAt(index): Returns the Unicode value of the character at a specified index.

    console.log(str.charCodeAt(1)); // Output: 101 (Unicode for 'e')
  • Modifying Strings

    concat(str1, str2, ...): Joins strings together.

    let greeting = "Hello";
    let name = "Alice";
    let message = greeting.concat(" ", name, "!");
    console.log(message); // Output: Hello Alice!

    replace(searchValue, newValue): Replaces occurrences of a specified value with another.

    let text = "The quick brown fox jumps over the lazy dog.";
    let newText = text.replace("fox", "cat");
    console.log(newText); // Output: The quick brown cat jumps over the lazy dog.

    toUpperCase(): Converts all characters to uppercase.

    console.log(str.toUpperCase()); // Output: HELLO, WORLD!

    toLowerCase(): Converts all characters to lowercase.

    console.log(str.toLowerCase()); // Output: hello, world!

    trim(): Removes whitespace from both ends of the string.

    let strWithWhitespace = "   Hello, world!   ";
    console.log(strWithWhitespace.trim()); // Output: Hello, world!
  • Extracting Substrings

    slice(startIndex, endIndex): Extracts a portion of a string.

    console.log(str.slice(7, 13)); // Output: world!
    • substring(startIndex, endIndex): Similar to slice, but arguments are treated as unsigned integers.
    • substr(startIndex, length): Extracts a substring based on starting index and length.
  • Checking Properties

    startsWith(searchString, position): Checks if a string starts with a specified substring.

    console.log(str.startsWith("Hello")); // Output: true

    endsWith(searchString, length): Checks if a string ends with a specified substring.

    console.log(str.endsWith("world!")); // Output: true

    includes(searchString, position): Checks if a string contains a specified substring.

    console.log(str.includes("world")); // Output: true
  • Other Useful Methods

    split(separator, limit): Splits a string into an array of substrings.

    let words = str.split(" ");
    console.log(words); // Output: ["Hello,", "world!"]

    indexOf(searchValue, fromIndex): Returns the index of the first occurrence of a specified value.

    console.log(str.indexOf("world")); // Output: 7

    lastIndexOf(searchValue, fromIndex): Returns the index of the last occurrence of a specified value.

    console.log(str.lastIndexOf("o")); // Output: 8

    length: Returns the length of the string.

    console.log(str.length); // Output: 13

Q5. What is string immutability? V. IMP.

Strings in JavaScript are considered immutable because you cannot modify the contents of an existing string directly.

var str = 'Interview';

//Creates a new string
str = str + 'Happy';

Q6. In how many ways you can concatenate strings?

Ways to concatenate strings

  • (+) Operator
  • Concat() method
  • Template literals
  • Join() method
// Declare two strings
let s1 = 'Hello';
let s2 = 'World';
// Concatenate using + operator
let r1 = s1 + s2;
console.log(r1); // Output: HelloWorld
// Concatenate using concat() method
let r2 = s1.concat(s2);
console.log(r2); // Output: HelloWorld
// Concatenate using template literals
let r3 = `${s1} ${s2}`;
console.log(r3); // Output: Hello World
// Concatenate using join() method
let strings = [s1, s2];
let r4 = strings.join('');
console.log(r4); // Output: Hello World