50+ JavaScript Coding Interview Questions, from ScriptVerse.DEV

Here are 50+ short interview questions in JavaScript!

Q1. Reverse the string:

Welcome to the Jungle

It should read:

Jungle the to Welcome

				
					let str = "Welcome to the Jungle";

					console.log(str.split(" ").reverse().join(" "));
				
			

Q2. Reverse the string:

Welcome to the Jungle

in the format:

elgnuJ eht ot emocleW

				
					let str = "Welcome to the Jungle";

					console.log(str.split("").reverse().join(""));
				
			

Q3. Given a string

Welcome to the Jungle

reverse each word in the sentence:

emocleW ot eht elgnuJ

				
					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?

				
					let a = [];

					console.log(Array.isArray(a));
				
			

Q5. How do you empty an array?

				
					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]

				
					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

				
					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 invoked as mul(3)(4)(5)

				
					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

				
					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" and b = "Diem"

				
					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?

				
					let arr = [1, undefined, null, false, null, 6, 9];

					console.log(arr.filter(Boolean));
				
			

Q12. How do you remove all the repititive elements from an array?

				
					let arr = [1, 1,1,2,7];

					console.log([... new Set(arr)]);
				
			

Q13. How do you convert an array into an object?

				
					let birds = ['Sparrow', 'Magpie', 'Crane'];

					console.log({...birds});
				
			

Q14. How do you convert an object into an array?

				
						const man = {
						    name: 'John Doe',
						    gender: 'Male'
						};

						console.log(Object.values(man));
				
			

Q15. How do you convert a string into an array?

				
					let language = 'English';

					console.log(language.split(''));
				
			

Q16. How do you flatten an array?

				
					let arr = [1, [2, 3], [4, 5], 6];

					arr.flat();

					// or

					[].concat(...arr);
				
			

Q17. How do you test if an object is empty?

				
					Object.entries(obj).length === 0
				
			

Q18. Given the string: "this is a string", find the number of "i", "s" and "t" occuring in the string.

				
					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?!

				
					let re = /^[A-Z]/;
					let str = "Lakme";

					console.log(re.test(str)); 
				
			

Q20. Test if all characters in a string are LOWERCASED or not?!

				
				    function isLower(s) {
				        var pattern = /^[a-z]+$/;
				        return pattern.test(s);
				    }
				
			

Q21. What is the Regex for the number format XXX-XXXXXXXX or XXXXXXXXXXX

				
					/\d{3}-?\d{8}/g
				
			

Q22. Select all words starting with the letter p.

				
					/\bp/g
				
			

Q23. Regex that accepts only numbers.

				
					/^\d+$/g 
				
			
or
				
					/^[0-9]+$/
				
			

Q24. Regex that accepts only characters.

				
					/^\[A-Za-z]+$/g 
				
			

Q25. Regex that accepts a 10-digit phone number.

				
					/^\d{10}$/g