Some tricky JavaScript concepts that you might encounter in an interview

Truthy vs Falsy Values

In JavaScript, when used inside a condition, a value is considered to be truthy if it yields in a true condition. Otherwise it is regarded as a falsy value. There are some fixed falsy values in JS: false, 0, -0, ‘’, null, undefined and NaN.

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

There is a subtle difference between ‘==’ and ‘===’ equal in JS. When used ‘==’, it only compares whether two values are equal or not. It doesn’t check the data type. For example, if string ‘1’ and number 1 are compared with ‘==’, it will return true. It automatically converts the string ‘1’ to a number data type. But this is not the case for ‘===’. Here both value and data types are compared. Hence, for the same previous example, the returned result will be false for ‘===’.

Null and Undefined

In JS, undefined is considered as a data type and null is considered as an object. That is, when we define a variable, but do not assign any value to it, we get undefined. On the other hand, we can set null as a value to any variable.

let demo;
console.log(demo); //shows undefined
console.log(typeof demo); //shows undefined
let demo = null;
console.log(demo); //shows null
console.log(typeof demo); //shows object

Finding the largest element in an array

let arr = [1,5,77,22,5,7,83,34,91,3];
let maxValue = arr[0];
for (let i = 0; i< arr.length; i++){
if(arr[i] > maxValue){
maxValue = arr[i];
//should print out '91'

Removing duplicate item from an array

let arr = [1,5,77,22,5,7,93,22,34,1,77,91,3];
let newArr = [];
for (let i = 0; i< arr.length; i++){
if(!newArr.includes(arr[i])) {
//expected output:'[1,5,77,22,7,93,34,91,3]'

Counting the number of words in a string

let str = 'My name is wreet';
const arr = str.split(' ');
//expected output: '4'

Reverse a string

let str = 'My name is wreet';
let reversedStr = '';
for(let i = str.length-1; i >= 0; i--){
reversedStr += str[i];
//expected output: 'teerw si eman yM'

Finding factorial of a number

function factorial(n){
let count = 1;
for(let i=1; i<=n; i++){
count *= i;
return count;
//expected output: '6'

Finding factorial of a number using recursion

function factorial(n){
if(n ===1){
return 1;
return n * factorial(n-1)
//expected output : '120'



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