Thought leadership from the most innovative tech companies, all in one place.

10 Tricky JavaScript Coding Interview Questions (And Solutions)

Some tricky questions in the coding interview you are going to face. These questions will look easy but there is something fishy. So today I am going to show you 10 tricky questions you should see before going to a coding interview.

1. Given a string, reverse each word in the sentence

var string = "Welcome to this Javascript Guide!";

// Output becomes !ediuG tpircsavaJ siht ot emocleW
var reverseEntireSentence = reverseBySeparator(string, "");

// Output becomes emocleW ot siht tpircsavaJ !ediuG
var reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");

function reverseBySeparator(string, separator) {
  return string.split(separator).reverse().join(separator);

2. How to empty an array in JavaScript?

var arrayList =  ['a', 'b', 'c', 'd', 'e', 'f'];

method 1

arrayList = [];

The above code will set the variable arrayList to a new empty array. This is recommended if you don't have references to the original array arrayList anywhere else because It will actually create a new empty array.

method 2

arrayList.length = 0;

Above code will clear the existing array by setting its length to 0. This way of empty the array also updates all the reference variable which points to the original array.

method 3

arrayList.splice(0, arrayList.length);

The above implementation will also work perfectly. This way of empty the array will also update all the references of the original array.

Note: If you are planning on applying for coding jobs and need help with your resume, you can find cheap resume services here.

3. How would you check if a number is an integer?

A very simple way to check if a number is a decimal or integer is to see if there is a remainder left when you divide by 1.

function isInt(num) {
  return num % 1 === 0;

console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false

4. Explain what a callback function is and provide a simple example.

A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed. Below is an example of a simple callback function that logs to the console after some operations have been completed.

function modifyArray(arr, callback) {
  // do something to arr here
  // then execute the callback function that was passed

var arr = [1, 2, 3, 4, 5];

modifyArray(arr, function() {
  console.log("array has been modified", arr);

5. Given two strings, return true if they are anagrams of one another

An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.

var firstWord = "Mary";
var secondWord = "Army";

isAnagram(firstWord, secondWord); // true

function isAnagram(first, second) {
  // For case insensitivity, change both words to lowercase.
  var a = first.toLowerCase();
  var b = second.toLowerCase();

  // Sort the strings, and join the resulting array to a string. Compare the results
  a = a.split("").sort().join("");
  b = b.split("").sort().join("");

  return a === b;

6. What will be the output of the following code?

var y = 1;
if (function f() {}) {
  y += typeof f;

Above code would give output 1undefined. If condition statement evaluate using eval so eval(function f() {}) which return function f() {} which is true so inside if statement code executes. typeof f return undefined because if statement code executes at run time, so statement inside if condition evaluated at run time.

7. What will the following code output?

(function() {
  var a = b = 5;


The code above will output 5 even though it seems as if the variable was declared within a function and can't be accessed outside of it. This is because

var a = b = 5;

is interpreted the following way:

var a = b;
b = 5;

But b is not declared anywhere in the function with var so it is set equal to 5 in the global scope.

8. What will the following code output?

for (var i = 0; i < 4; i++) {
  setTimeout(() => console.log(i), 0)

The classic pitfall here is the Zero delays. setTimeout(callback, 0) doesn't mean that the callback will be fire after zero milliseconds.

Here’s what happen on the event loop side:

  1. Current Call Stack is set to the first setTimeout().

  2. windows.setTimeout() is considered as a Web APIs (for better Non-Blocking I/O). So the call stack sends this part of code to correct Web APIs. After 0 milliseconds, the callback (here an anonymous function) would be sent to the Queue (not to the call stack).

  3. As the call stack is free, for-loop can continue to the second setTimeout …(repeat after we meet this condition i < 4)…

  4. Now the loop is over and i === 4. JS can now execute the callback queue one by one. Each console.log(i) will print the 4.

9. Palindrome Problem

A palindrome is a word, sentence, or other types of character sequence that reads the same backward as forward. For example, “racecar” and “Anna” are palindromes. “Table” and “John” aren’t palindromes, because they don’t read the same from left to right and from right to left.

const palindrome = str => {
  // turn the string to lowercase
  str = str.toLowerCase()
  // reverse input string and return the result of the
  // comparisong
  return str === str.split('').reverse().join('')

10. Find the Vowels

This is probably one of the less challenging challenges (no pun intended) — in terms of difficulty — but that doesn’t detract from the fact that you could come across it during a job interview. It goes like this.

const findVowels = str => {
  let count = 0
  const vowels = ['a', 'e', 'i', 'o', 'u']
  for(let char of str.toLowerCase()) {
    if(vowels.includes(char)) {
  return count

That’s all for today. Thanks everyone for reading it till the end. Keep learning :)

Continue Learning