10 Interview Questions for a JavaScript developer

Farzana Nipa
6 min readNov 5, 2020

--

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.

//When value is number and grater than 0
const number = 5;
if (number > 0) {
console.log("Truthy");
}
else{
console.log("Falsy");
}
console.log(number); // Truthy// when value is 0const number = 0;
if (number) {
console.log("Truthy");
}
else{
console.log("Falsy");
}
console.log(number); // Falsy//When value is string
const name = "Mahveer";
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);//Truthy//when value is whitespace
const name = " ";//whitespace
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);
//when value is empty
const name = "";
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);//Falsy//when value is undefined
let name;//undefined
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);//Falsy//when value is null
let name = null;
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);//Falsy

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.

const name = false;
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);//Falsy
//false as a string
const name = 'false';
if (name) {
console.log("Truthy");
}
else
{
console.log("Falsy");
}
console.log(name);//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’.

//when value is undefinedlet name;console.log(name);//undefined

We can see another example:

function sum(num1, num2){
console.log(num1 + num2)
}
const result = sum(15, 20);
console.log(result);//undefined

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.

function sum(x, y) {console.log(x, y);}const result = sum(10);console.log(result);// 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:

const first = 1;const second = true;if (fast == second) {
console.log("True");
}
else{
console.log("False");
}
Result: Trueconst first = 0;
const second = false;
if (fast == second) {
console.log("True");
}
else{
console.log("False");
}
Result: True

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

const first = 1;const second = true;if (fast === second) {
console.log("True");
}
else{
console.log("False");
}
Result: Falseconst first = 0;
const second = false;
if (fast == second) {
console.log("True");
}
else{
console.log("False");
}
Result: False

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.

function add(num1, num2) {let outpt = num1 + num2;return output;}console.log(output); //errorconsole.log(add(20, 30));//50

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.

let bonus = 10;
function add(num1, num2) {
let outpt = num1 + num2 + bonus;
console.log(bonus);// 10
return output;}console.log(bonus); //10console.log(add(20, 30));//60

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:

const number = [55, 35, 65, 95, 25];let max = number[0];for (let i = 0; i < number.length; i++) {const element = number[i];if(element > max){max = element}}console.log(max);//95

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.

const number = [55, 35, 65, 95, 25];let sum = 0;for (let i = 0; i < number.length; i++) {const element = number[i];sum = sum + element}console.log(sum);//275

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:

const number = [55, 35, 65, 95, 25, 35, 55];let newNumber = [];for (let i = 0; i < number.length; i++) {const element = number[i];let index = newNumber.indexOf(element);if(index === -1){newNumber.push(element)}}console.log(newNumber);//[ 55, 35, 65, 95, 25 ]

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:

const myself = "I am a front end web developer"let count = 0;for (let i = 0; i < myself.length; i++) {const char = myself[i];if(char === " " && myself[i-1] !== " "){count++;}}count++;console.log(count);//7

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:

function reverseText(string) {let reverse = "";for (let i = 0; i < string.length; i++) {const char = string[i];reverse = char + reverse;}return reverse;}const proverb = "Man is mortal";const output = reverseText(proverb);console.log(output);//latrom si naM

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:

function isPrime(n) {for (i = 2; i < n; i++) {if (n % i === 0) {return "Not a prime number";}}return "A prime number"}const result = isPrime(7);//A prime numberconst result = isPrime(8);//Not a prime numberconsole.log(result);

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.

--

--

Farzana Nipa

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