10 Interview Questions for a JavaScript developer

Truthy and Falsy:

‘Which terms are truthy and which are falsy’ is a common interview question for a JavaScript developer. Let’s try to clear the concept of truthy and falsy.

When the value of a declared variable is a number, then the numbers greater than 0 are truthy and if the number is smaller than 0 or equal to 0 then it would be falsy. But if we get 0 as a string then it is truthy.

In the case of string if anything assigned to a variable it is truthy even there is a whitespace between the double quote, that is also truthy. We can see some examples for better visualization.

If the assigned value is false then the result is falsy but the value ‘false’ comes with a quotation then it is truthy because that time ‘false’ is a string and any string is truthy.

Empty array []; and empty object {}; are also truthy.

Undefined vs Null:

Undefined: ‘What is the difference between undefined and null?’ Sometimes interviewers ask this question to a JavaScript developer candidate.

First, I want to explain a bit in which cases we get the value ‘undefined’.

We can see another example:

As there is nothing returned in this function the result got undefined.

When the function parameter is not defined, by default the value would be undefined.

Here, the value of ‘x’ is 10 but the value of ‘y’ is not defined so we got the result undefined.

Null: Null means it does not exist. If we want a value null we have to set it explicitly.

Double equal(==) vs Triple equal(===):

To describe the difference between double equal and triple equal, first, we can take help from an example:

Now what occurs if we check the condition through (===)

We can see the results are different. So what is the reason behind this? well, the reason is when we check a condition through (==), it doesn’t check the type but in triple (===), the data type also checked. By default in JavaScript the 1 is considered as true and 0 is false. So when we checked by (==), we got the result true. But 1 and 0 are numbers and ‘true’, ‘false ’ are booleans so in (===) types are not matched.

Scope:

Let’s describe what is scope and how it is used.

Here, the result of output is showing an error because the output is declared into a function, so it is a block scope. And a block scope couldn’t be called from outside. Let’s see another example to understand the global scope.

Here we can access the value of bonus inside the function and as well as outside. Because the bonus variable is declared outside the function. So it is called global scope and global scope is accessible from anywhere.

Find the largest element of an array:

At the interview board, a candidate not only has to give the theoretical question’s answer but also has to solve the given problems. Amongst them ‘Find the largest element of an array’ is one of the common questions.

Here I am giving an example to find the largest element of an array:

Here, the first element of the array is assigned as the max number and through the for loop, we have got all the elements then one by one the element is checked through an if statement. If the element is greater than the max number then the max variable value would bt the element.

Sum of all numbers in an array:

In JavaScript there are several methods to get the sum of all numbers in an array. Here I am showing you how can we get the sum of numbers.

Here, the initial value of the sum variable is assigned as 0. Then through the for loop method of JavaScript when we get a number, that is added with the previous value of sum. Thus we can get the sum of the numbers in an array.

Remove duplicate item from an array:

Removing duplicate items can be another problem-solving question.

We can see this through an example:

First, we have got the elements of the array through JavaScript for loop method. Then checked the index number of the element and if the index number hasn’t found in the new array then the number will be pushed into the new array. This way we can remove the duplicate numbers from an array.

Count the number of words in a string:

I am trying to explain how to count the number of words in a string with the example given below:

Here, the method is working by counting the number of whitespaces. By mistake or intentionally one can give two whitespaces at a time. To fix it, this is checked through ‘myself[i-1]’ condition. Generally, no one gives whitespace at the end of a sentence. To count the last word ‘count++’ is used after the for loop.

Reverse a string:

If the given problem is how to reverse a string, my solution would be this:

First, I have made a function named ‘reverseText’. In this function, through for loop I have got the characters and they are added to the declared variable ‘reverse’. With the method ‘reverse = char + reverse’, every character added to the new string, before the previous character.

Check whether a number is a Prime Number or not:

In JavaScript we can check if a number is a prime number or not. Let’s see how we can check this:

If a number is dividable only by ‘1’ and that given number, the number would be a prime number. The function ‘isPrime’ checked all the conditions and give the proper answer of the question.

I am a professional front-end web developer, passionate about coding. I always keep myself learning new things . I love what i do.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store