DEV Community

Cover image for Common interview questions that you need to know as a junior JavaScript developer.
Mir Hussain
Mir Hussain

Posted on • Updated on

Common interview questions that you need to know as a junior JavaScript developer.

Verbal questions.

Differences between '=' , '==' & '==='.

In JavaScript Equal to ( = ) is an assignment operator and Double or Triple equals ( ==, ===) are comparison operators. We use a single equal sign to assign a value to a variable by placing the variable name on the left side of the equal sign and the value on the right side of the equal sign.

Both the double equals ( == ) and triple equals ( === ) are used to compare two values. But there is a difference between them and the triple equal operator is safer.

By using double equals, JavaScript tries to match the value, and it does not bother about the type of value. So first it will convert the type of value to match the type of the values and then will compare. For example, if you compare 23 (number) with '23' (string) using double equals, JavaScript will convert the string to a number and then will compare it, as a result, the return value will be true.

"23" == 23 // true
Enter fullscreen mode Exit fullscreen mode

On the other hand, triple equals ( === ) avoid these kinds of conversions. It checks both the value and type. As a result, if you compare 23 (Number) with ‘23’ (String) the return value will be false.

"23" == 23 // false
Enter fullscreen mode Exit fullscreen mode

Truthy & Falsy values.

In JavaScript truthy value means, a value that is considered as true when encountered in a Boolean. The same rule applies to falsy value.

Truthy values are:

true, empty array " [] ", empty object "{}", number ( Nagative and positive number including float number, exept 0 and -0), BigInt, new Date(), -infinity and infinity.

Falsy values are:

false, 0, -0, 0n, empty string (""), null, undefiend and NaN.

What are "null" and "undefined"?

In JavaScript null is an object, where undefined is a type. null means empty. It can be assigned as a value where you don't want to assign any specific value. undefined means when a variable is declared but no value is assigned to it.

What is DOM ??

Document Object Model is a programming interface for HTML and XML. A webpage is an HTML document. We can't manipulate this document with a programming language. So DOM comes into play. DOM is an object-oriented representation of an HTML document. It represents the document as nodes and object which can be manipulated by a programming language such as JavaScript.

JavaScript arrow function.

JavaScript arrow function is shorthand of normal function in JavaScript. But it has some limitations too.

*Does not have its own bindings to this or super, and should not be used as methods.
*Does not have arguments, or new.target keywords.
*Not suitable for call, apply and bind methods, which generally rely on establishing a scope.
*Can not be used as constructors.
*Can not use yield, within its body.

//Traditional Named Function with arguments
function add(a, b) {
  return a + b; 
} 

//Arrow Function 
add (a, b) => {
 return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Practical tasks

Find the largest element of an array.

Math.max() method returns the largest element.You can use this for an array with help of spread operator (...)

const numbers = [1, 2, 3]

console.log(Math.max(...numbers)) // 3 
Enter fullscreen mode Exit fullscreen mode

Remove duplicate elements from an array.

Easiest way to remove duplicates from an array is to use filter() array method. filter() returns a new array of elements. You can filter out duplicate elements by their index number.

const names = ["John", "Smith", "John", "David"]
function removeDuplicate(data) {
  return data.filter((value, index) => {
    data.indexOf(value) === index
  })
}

console.log(removeDuplicate(names)); // ["John", "Smith", "David"]
Enter fullscreen mode Exit fullscreen mode

Count the number of words in a string.

If you are not an expert in RegEx like me, you can simply split the string with space and use .length method to count the words.

function WordCount(str) { 
  return str.split(" ").length;
}

console.log(WordCount("hello world"));
Enter fullscreen mode Exit fullscreen mode

Calculate the factorial of a number using for loop.

Well, if you are not familiar with factorial, you need to know what the heck is factorial. Factorial is a mathematical term. It means multiplying a selected number down to 1. For example, the factorial of 5 would be 5*4*3*2*1 = 120. There is a condition that the number must be a positive integer and factorial of 0 is equal to 1 (Just don't ask why.)

Okay so let's get our conditions straight. No negative number and 0 = 1.

function factorialize(num) {
  if (num < 0) {
    console.log("Wrong Input")
  }
  if (num === 0 || num === 1)
    return 1;
  for (let i = num - 1; i >= 1; i--) {
    num *= i;
  }
  return num;
}
factorialize(5);
Enter fullscreen mode Exit fullscreen mode

In this code, we will start from 5 all the way down to 1. In the first iteration, the value of 'i' would be num - 1 which is 4. We decrement 'i' by 1 after each iteration. We will store the number in the num variable and return it in the end.

Check whether a number is a prime number or not.

A prime number is a positive integer that is only divisible by 1 and itself. For example, 2, 3, 5, 7, 11 are the first few prime numbers.

function test_prime(n)
{

  if (n===1)
  {
    console.log("1 is neither prime nor composite number.")
  }
  else if(n === 2)
  {
    console.log("Prime number")
  } else {
    for(let x = 2; x < n; x++)
    {
      if(n % x === 0)
      {
        console.log("Not a prime number")
      }
    }
    console.log("Prime number") 
  }
}

test_prime(37)
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
reaxul profile image
Reaxul Alavhi

Your 'removeDuplicate' function attempts to filter out duplicate values from the given array (names). However, there's a small issue in your code. The filter function expects a 'boolean' return value to determine whether to include the current element in the filtered array. Currently, your filter function doesn't return anything, which implicitly returns undefined. To fix this, you should explicitly return the result of the comparison in your filter callback.

Collapse
 
uisubho98 profile image
Subhojit Dutta

Ahh! mir bhai is king!!! thanks for this blog