JavaScript Basic Interview Questions for Mastering String

Q1:- Reverse a String Question: Write a function to reverse a string. Ans function reverseString(str) { return str.split('').reverse().join(''); } console.log(reverseString('hii')); // Output: "iih" Code Q2:- Check for Palindrome Question: Determine if a given string is a palindrome. Ans function Check_Palindrome(str){ let reversed = str.split('').reverse().join(''); if(str === reversed){ console.log(`${str} is a Palindreme Number`) }else{ console.log(`${str} is not a Palindreme Number`) } } console.log(Check_Palindrome('11a')) Code Q3:- How do you remove whitespace from both ends of a string in JavaScript? Code Q4:- How do you extract a section of a string in JavaScript? You can use the slice() method in JavaScript to extract a section of a string and return it as a new string. Example: let str = "Hello World!"; let slicedStr = str.slice(0, 5); console.log(slicedStr); // "Hello" Code Q5:- Sort a String in JavaScript You can use the split(), sort(), and join() methods to sort a string in JavaScript. Example: let str = "IHGFEDCBA" let Shorted_String = str.split('').sort().join() console.log(Shorted_String) // A,B,C,D,E,F,G,H,I Code Q6:- Write Separate Functions for Each Social Media Platform to Extract Usernames from the URLs Write separate functions for each of the following social media platforms that will extract the username from the URL. The URL format for each platform is provided below. Each function should take a URL as input and return the corresponding username from that URL. Question: Instagram URL format: https://www.instagram.com/{username}/ Facebook URL format: https://www.facebook.com/{username} LinkedIn URL format: https://www.linkedin.com/in/{username}/ Twitter URL format: https://twitter.com/{username} YouTube URL format: https://www.youtube.com/c/{username} Pinterest URL format: https://www.pinterest.com/{username}/ Snapchat URL format: https://www.snapchat.com/add/{username} TikTok URL format: https://www.tiktok.com/@{username} Reddit URL format: https://www.reddit.com/u/{username}/ GitHub URL format: https://github.com/{username} WhatsApp URL format: https://wa.me/{username} Tumblr URL format: https://{username}.tumblr.com/ Spotify URL format: https://open.spotify.com/user/{username} Skype URL format: https://join.skype.com/{username} Flickr URL format: https://www.flickr.com/photos/{username}/ Exaple function getInstagramUsername(url) { return url.split('instagram.com/')[1].split('/')[0]; } Code

Apr 15, 2025 - 04:53
 0
JavaScript Basic Interview Questions for Mastering String

Q1:- Reverse a String

  • Question: Write a function to reverse a string.

Ans

function reverseString(str) {

    return str.split('').reverse().join('');
  }

  console.log(reverseString('hii')); // Output: "iih"


Code

Q2:- Check for Palindrome

  • Question: Determine if a given string is a palindrome.

Ans

function Check_Palindrome(str){

let reversed = str.split('').reverse().join('');
if(str === reversed){
    console.log(`${str} is a Palindreme Number`)
}else{
    console.log(`${str} is not a Palindreme Number`)
}

}

console.log(Check_Palindrome('11a'))

Code

Q3:- How do you remove whitespace from both ends of a string in JavaScript?

Code

Q4:- How do you extract a section of a string in JavaScript?

You can use the slice() method in JavaScript to extract a section of a string and return it as a new string.

Example:

let str = "Hello World!";
let slicedStr = str.slice(0, 5);
console.log(slicedStr); // "Hello"

Code

Q5:- Sort a String in JavaScript

You can use the split(), sort(), and join() methods to sort a string in JavaScript.

Example:

let str = "IHGFEDCBA"
let Shorted_String = str.split('').sort().join()
console.log(Shorted_String) // A,B,C,D,E,F,G,H,I

Code

Q6:- Write Separate Functions for Each Social Media Platform to Extract Usernames from the URLs

Write separate functions for each of the following social media platforms that will extract the username from the URL. The URL format for each platform is provided below. Each function should take a URL as input and return the corresponding username from that URL.

  • Question:
  1. Instagram

    • URL format: https://www.instagram.com/{username}/
  2. Facebook

    • URL format: https://www.facebook.com/{username}
  3. LinkedIn

    • URL format: https://www.linkedin.com/in/{username}/
  4. Twitter

    • URL format: https://twitter.com/{username}
  5. YouTube

    • URL format: https://www.youtube.com/c/{username}
  6. Pinterest

    • URL format: https://www.pinterest.com/{username}/
  7. Snapchat

    • URL format: https://www.snapchat.com/add/{username}
  8. TikTok

    • URL format: https://www.tiktok.com/@{username}
  9. Reddit

    • URL format: https://www.reddit.com/u/{username}/
  10. GitHub

  • URL format: https://github.com/{username}
  1. WhatsApp
  • URL format: https://wa.me/{username}
  1. Tumblr
  • URL format: https://{username}.tumblr.com/
  1. Spotify
  • URL format: https://open.spotify.com/user/{username}
  1. Skype
  • URL format: https://join.skype.com/{username}
  1. Flickr
  • URL format: https://www.flickr.com/photos/{username}/

Exaple


function getInstagramUsername(url) {
  return url.split('instagram.com/')[1].split('/')[0];
}

Code