100+ JavaScript Coding Interview Questions, from ScriptVerse.DEV
Here are 100+ short interview questions in JavaScript!
-
Q1. Reverse the string:
Welcome to the JungleIt should read:
Jungle the to WelcomeAnswer/Solution
let str = "Welcome to the Jungle"; console.log(str.split(" ").reverse().join(" ")); -
Q2. Reverse the string:
Welcome to the Junglein the format:
elgnuJ eht ot emocleWAnswer/Solution
let str = "Welcome to the Jungle"; console.log(str.split("").reverse().join("")); -
Q3. Given a string
Welcome to the Junglereverse each word in the sentence:
emocleW ot eht elgnuJAnswer/Solution
let str = "Welcome to the Jungle"; const revStr = (str, separator) => { return str.split(separator).reverse().join(separator); }; let t = revStr(str, ""); console.log(revStr(t, " ")); -
Q4. How do you check if a given object is an array?
Answer/Solution
let a = []; console.log(Array.isArray(a)); -
Q5. How do you empty an array?
Answer/Solution
let a = [1, 2, 3]; a.length = 0; // or a.splice(0, a.length); console.log(a); -
Q6. Duplicate the array
a = [1,2,3]Answer/Solution
let a = [1, 2, 3]; console.log(a.concat(a)); // [1,2,3,1,2,3] -
Q7. Write a function to check the equality of two arrays
Answer/Solution
const areEqual = (a, b) => { return Array.isArray(a) && Array.isArray(b) && (a.length === b.length) && (a.every((itm, idx) => itm === b[idx])); }; -
Q8. Write a function called
mul()which returns the product of three numbers and is invoked asmul(3)(4)(5)Answer/Solution
const mul = (a) => (b) => (c) => a * b * c; console.log(mul(1)(2)(3)); -
Q9. Write a function to check if two strings are an anagram
Answer/Solution
let word = "peach"; let anotherWord = "cheap"; const isAnagram = (f, s) => { let a = f.toLowerCase(); let b = s.toLowerCase(); a = a.split("").sort().join(""); b = b.split("").sort().join(""); return a.localeCompare(b) === 0 ? true : false; }; console.log(isAnagram(word, anotherWord)); -
Q10. Swap the values of the two strings
a = "Carpe"andb = "Diem"Answer/Solution
let a = "Carpe"; let b = "Diem"; [a, b] = [b, a]; console.log(a); console.log(b); -
Q11. How do you remove all the falsy values from the array?
Answer/Solution
let arr = [1, undefined, null, false, null, 6, 9]; console.log(arr.filter(Boolean)); -
Q12. How do you remove all the repetitive elements from an array?
Answer/Solution
let arr = [1, 1, 1, 2, 7]; console.log([...new Set(arr)]); -
Q13. How do you convert an array into an object?
Answer/Solution
let birds = ['Sparrow', 'Magpie', 'Crane']; console.log({ ...birds }); -
Q14. How do you convert an object into an array?
Answer/Solution
const man = { name: 'John Doe', gender: 'Male' }; console.log(Object.values(man)); -
Q15. How do you convert a string into an array?
Answer/Solution
let language = 'English'; console.log(language.split('')); -
Q16. How do you flatten an array?
Answer/Solution
let arr = [1, [2, 3], [4, 5], 6]; arr.flat(); // or [].concat(...arr); -
Q17. How do you test if an object is empty?
Answer/Solution
Object.entries(obj).length === 0 -
Q18. Given the string:
"this is a string", find the number of "i", "s" and "t" occurring in the string.Answer/Solution
let a = "this is a string"; function countLetters(str, letter) { const re = RegExp(letter, 'g'); const count = str.match(re).length; return count; } console.log(countLetters(a, 'i')); // 3 console.log(countLetters(a, 's')); // 3 console.log(countLetters(a, 't')); // 2 -
Q19. Test if the first character of a string is CAPITALIZED or not?!
Answer/Solution
let re = /^[A-Z]/; let str = "Lakme"; console.log(re.test(str)); -
Q20. Test if all characters in a string are LOWERCASED or not?!
Answer/Solution
function isLower(s) { var pattern = /^[a-z]+$/; return pattern.test(s); } -
Q21. What is the Regex for the number format
XXX-XXXXXXXXorXXXXXXXXXXX?Answer/Solution
/\d{3}-?\d{8}/g -
Q22. Select all words starting with the letter
p.Answer/Solution
/\b p /g -
Q23. Write a regex that accepts only numbers.
Answer/Solution
/^\d+$/gor
/^[0-9]+$/ -
Q24. Write a regex that accepts only characters.
Answer/Solution
/^\[A-Za-z]+$/g -
Q25. Write a regex that accepts a 10-digit phone number.
Answer/Solution
/^\d{10}$/g -
Q26. What will be the output of the following?
if (false) { var a = 1; let b = 2; } console.log(a); // undefined console.log(b); // Reference ErrorAnswer/Solution
undefinedandReference Error -
Q27. What will be the output of the following?
let id = Symbol("id'"); console.log(typeof id);Answer/Solution
symbol -
Q28. What will be the output of the following?
(function () { var a = b = 1; })(); console.log(a); // Reference Error console.log(b); // 1Answer/Solution
Reference Errorand1 -
Q29. What will be the output of the following?
this.n = 2; // 'this' refers to global window object var number = { n: 3, getN: function () { return this.n; } }; console.log(number.getN()); // 3this.n = 2; // 'this' refers to global window object var number = { n: 3, getN: () => { return this.n; } }; console.log(number.getN()); // 2Answer/Solution
3and2 -
Q30. What is the difference between
Object.freeze()andObject.seal()?Answer/Solution
Object.freeze()prevents any changes to existing properties and prevents adding or removing properties.Object.seal()prevents adding or removing properties, but existing writable properties can still be changed. -
Q31. What is the difference between
.match()and.matchAll()in Regular Expressions?Answer/Solution
.match()returns the occurrence as strings, and.matchAll()returns an iterator of all matches as arrays. -
Q32. What is the output of
console.log(typeof typeof 1);Answer/Solution
string -
Q33. What is the output of
console.log(0.1 + 0.2 == 0.3);Answer/Solution
false -
Q34. How do you check if a number is an Integer?
let n = 1; console.log(Number.isInteger(n));Answer/Solution
true -
Q35. What will be the result of the following?
let n = 1.0; console.log(Number.isInteger(n));Answer/Solution
true -
Q36. A function is defined as
function f() {}What will be its Boolean output? What is
typeof f?function f() {}; console.log(f ? 1 : 2); // 1 console.log(typeof f); // functionAnswer/Solution
true -
Q37. What will be the result of the program?
var y = 1; if (function f() {}) { y += typeof f; } console.log(y); // 1undefinedAnswer/Solution
1undefined