JavaScript Interview Questions for Freshers / Beginners – 2021-2022

JavaScript is undoubtedly the most popular programming language currently in the world. It has undergone so many number of changes in the past decade. Several new concepts, semantics, wrappers to the old syntaxes were added to the programming language. Things which used to be important earlier were not being considered as important now.

Many of the Interview Questions material available out there is not reflecting these changes. Some of the questions which are common in these questions such as ‘What is the difference between Java and JavaScript’ belongs to the period when JavaScript used to be not so popular. But such questions may not make much sense in the current scenario, especially for freshers.

Here we will try to provide the interview questions based on the trends and will keep on updating as these trends changes or with the questions based on new updates to the programming language.

We have recently started adding the questions. So this page will be updated frequently. Keep checking for new updates.
  1. How many Data Types are there in JavaScript ?
  2. Tell me more about Primitive/Value based Data type in JavaScript
  3. How to Create a variable of BigInt data type
  4. Tell me briefly about Symbol datatype
  5. What are ES5, ES6, ES7 etc., Are they versions of JavaScript ?
  6. What is the general style of creating variables?
  7. If you see a variable name in all capital letters, what type of variable it is ?
  8. If you see a variable name starting with a capital letter, what type of variable it is ?
  9. If you see a variable name starting with underscore, what type of variable it is ?
  10. If you see a variable name ending with dollar sign ($), what type of variable it is ?
  11. What is the difference between Undefined and Null ?
  12. What is the type of null in JavaScript
  13. Do we have Binary Number System in JavaScript ?
  14. How many types of Numbers are there in JavaScript ?
  15. If the value of a number is 011, what type of number it is ?
  16. How to Create a Binary Number in JavaScript ?
  17. How to convert Decimal number to Hexadecimal number ?
  18. What do we get when we divide 100 / or some number by zero ?
  19. What do we get when we divide zero by zero (0/0) ?
  20. Is Infinity is equals to Infinity?
  21. Is NaN is equals to NaN?
  22. Is Infinity is equals to Number object’s positive infinity ?
  23. Is Infinity is equals to Number object’s negative infinity ?
  24. How to check if a number is Infinite ?
  25. Is Zero is equals to Zero n (0n), which is a BigInt ?
  26. What is the difference between search and indexOf ?
  27. What is the difference between indexOf second argument and lastIndexOf second argument ?
  28. What is the difference between substring and substr ?
  29. How to check if a String starts with a specific character ?
  30. How to check if a string ends with a specific character ?
  31. What is the difference between double equals and triple equals ?
  32. What are all the false values in JavaScript ?
  33. How to add an item at the end of an Array ?
  34. How to add an element at the beginning of an Array ?
  35. How to remove last element from an Array ?
  36. How to remove the first element from an array ?
  37. How to find the position of an element in an array ?
  38. How do you check if an element exists in an array ?
  39. How do you merge two arrays in JavaScript ?
  40. How to convert a JavaScript object to JSON format ?
  41. How to convert a JSON string to a JavaScript object ?
  42. How many types of functions are there in JavaScript ?

Primarily JavaScript have two data types. Traditionally these data types used to be referred as Primitive and Non-Primitive data types. The more appropriate way to call them than Primitive and Non-Primitive is, Value Based & Reference Based data types. The Value based variables holds the actual value of the variable, whereas the Reference based variables holds the value of the memory location which points to the actual value of the variable.

The Value based or the primitive data type variables holds the actual value of the variables. Numbers, BigInt, Strings, Booleans, Undefined, Null and Symbols falls under this category. Symbols were introduced with ES6 standards of JavaScript and BigInt data types are not very common.

BigInt data type variables are relatively rarely used in JavaScript. To create a variable of big int type, we can append n at the end of the number. We can also use the constructor function BigInt() to create a variable.

var x = 100n
var y = BigInt(1)

console.log(typeof x, typeof y) // we get bigint bigint

Symbol is a Value based / Primitive data type introduced in the ES2015 or ES6 standards of JavaScript. Currently they are primarily used when Unique Values are required in JavaScript, since one Symbol will not be equal to another Symbol.

var x = Symbol();
var y = Symbol();

console.log(x == y) // we get false

No, they are not JavaScript versions. They are the Standards set by ECMA (European Computer Manufacturers Association). When they set Standards all browsers have to follow/implement those standards. Some browsers may take more time to implement and some may implement quickly. That’s why developers check for the browser compatibility whenever they want to use something from the latest standards.

Earlier they used to set the standards once in several years. Now, they are doing it every year. 99.99% these standards are backward compatible.

Generally we use Camel Case notation for variable names in which the variable name starts with a small letter and each new word starts with a capital letter. For example myVariableName. This is not must, but all developers follows the same style.

Generally JavaScript developers uses all capital letters to create Constant Variable. Though this is not must, it is generally considered as the good practice and followed by almost all developers.

Generally we give first letter as capital letter for Constructor functions and Class names. This is not must, but you will see the same pattern everywhere.

JavaScript do not have the concept of private variables. So developers generally creates variables starting with underscore (_), to indicate that, they want to use this variable as a private variable. Its only a naming convention practiced by many developers and it do not make that particular variable private. It only shows the intention of the developer.

If the variable name starting with an underscore is a parameter name, then it means that we are not using that variable inside the block.

example:

let myArray = [1, 2, 3];

myArray.forEach((_) => {})

Its an Observable. Observables are not part of JavaScript, but they are part of JavaScript library RxJS. Its a common convention to give variable names ending with dollar sign for observable names.

Undefined means, the variable is defined (or declared), but the value is not assigned yet. Null means, the value of the variable is set to null explicitly. Another difference is the type of undefined is undefined but the type of null is an Object. Null is a special type of Object in JavaScript.

var x;
console.log(typeof x); // we get undefined
var y = null;
console.log(typeof y) // we get object

The type of Null is an object.

ES5 JavaScript did not have Binary Number system. From ES6, we have Binary numbers in JavaScript.

In ES5, we had three types of numbers. Octal, Decimal and Hexadecimal. From ES6 onward, we also have Binary number system. So currently we have four types of number systems in JavaScript.

It is an Octal number. Octal numbers starts with 0 in JavaScript. In ES6+ JavaScript, we can also use 0o (zero o) to create an Octal number.

To create a binary number, the number needs to be preceded by 0b (zero b).

var x = 0b111;

console.log(x); // we get 7

We can convert a decimal number to hexadecimal number in JavaScript by using toString method which accepts radix number as its parameter. Since it is only radix, we can convert to any other base number, not just Binary, Octal or Hexadecimal.

example:

let x = 20;

console.log(x.toString(16)); // we get 14
console.log(x.toString(9)); // we get 22

Infinity.

console.log(100 / 0); // we get Infinity

NaN. Any number by zero is Infinity. Zero divided by zero is NaN.

console.log(0 / 0); // we get NaN

Yes. They are equal.

console.log(Infinity == Infinity); // we get true
console.log(Infinity === Infinity); // we get true

No. They are not equal. One NaN is not equals to anything in JavaScript, not even to another NaN.

console.log(NaN === NaN); // we get false
console.log(NaN == NaN); // we get false

Yes. They are equal

console.log(Infinity == Number.POSITIVE_INFINITY); // we get true

No. They are not equal. But -Infinity is equals to Number object’s negative infinity.

console.log(Infinity == Number.NEGATIVE_INFINITY); // we get false
console.log(-Infinity == Number.NEGATIVE_INFINITY); // we get true

We can check if a number is infinite by using the isFinite method of Number object.

example:

console.log(Number.isFinite(Number.MAX_VALUE)) // we get true
console.log(Number.isFinite(Number.MAX_VALUE * 2)) // we get false

With double equals (==) they are equal, with triple equals (===), they are not equal because, one is of type number and another one is BigInt.

console.log(0 == 0n); // we get true
console.log(0 === 0n); // we get false

Both of them returns the position of the string we are searching for. In indexOf method, we can also pass the second parameter which is the starting position of the search.

example:

let myString = "Hello World";

console.log(myString.search('o')); // we get 4
console.log(myString.indexOf('o', 5)); // we get 5

Another difference between search and indexOf is, we cannot use Regular Expressions with indexOf method.

example:

let myString = "howtoJS";
let regEx = /[A-Z]/g;

console.log(myString.indexOf(regEx)); // we cannot use, we get -1
console.log(myString.search(regEx)); // we get 5

In indexOf method, the second argument is the number of the position from which we want to search. In lastIndexOf method, the second argument is the number of the position till which we want to search.

example:

let myString = "howtoJS";

console.log(myString.indexOf('o')); // we get 1
console.log(myString.indexOf('o', 3)); // we get 4
console.log(myString.lastIndexOf('o')); // we get 4
console.log(myString.lastIndexOf('o', 3)); // we get 1

In substring method, the second argument tells the position of the string till which we want to slice. In substr method, the second argument is the length of the string that we want to slice.

example:

let myString = "how to JS";

console.log(myString.substring(7, 9)); // we get JS
console.log(myString.substr(7, 2)); // we get JS

We can check if a string starts with a specific character in JavaScript by using startsWith method or charAt method or indexOf method or by using the string as an array to access the first character.

example:

let myString = "HowToJS";

console.log(myString.startsWith('H')); // we get true
console.log(myString.charAt(0) === 'H'); // we get true
console.log(myString[0] === 'H'); // we get true
console.log(myString.indexOf('H') === 0); // we get true

We can check if a string starts with a specific character in JavaScript by using endsWith method or charAt method or indexOf method or by using the string as an array to access the first character.

example:

let myString = "HowToJS";

console.log(myString.endsWith('S')); // we get true
console.log(myString.charAt(myString.length - 1) === 'S'); // we get true
console.log(myString[(myString.length - 1)] === 'S'); // we get true
console.log(myString.indexOf('S') === (myString.length - 1)); // we get true

Double equals to checks for Abstract Equality whereas tripe equals checks for exact equality. In triple equals comparison, if the two values we are comparing are of different data type it returns false, with double equals JavaScript may convert one or the other variable to another data type to make the comparison.

example:

var x = 100;
var y = '100';

console.log(x == y); // we get true
console.log(x === y); // we get false

In the above example, with triple equals comparison, since x and y are of different data type, it returns false. With double equals, JavaScript first converts the string variable to a number, then performs the comparison.

Boolean false, 0 (zero), ”/”” (empty strings), undefined, null, NaN (Not a Number) are considered as false in JavaScript. Everything else apart from these is true.

We can add an element to an array at the ending position by using the push method.

var myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray); // we get [ 1, 2, 3, 4 ]

We can add an element to an array at the ending position by using the unshift method.

var myArray = [1, 2, 3];
myArray.unshift(0);
console.log(myArray); // we get [ 0, 1, 2, 3 ]

We can remove the last item from an array by using the pop method.

example:

let myArray = [1, 2, 3];
myArray.pop();
console.log(myArray); // we get [1, 2]

We can remove the last item from an array by using the shift method.

example:

let myArray = [1, 2, 3];
myArray.shift();
console.log(myArray); // we get [2, 3]

We can find the position of an element by using the indexOf method of an array.

example:

var myArray = ['a', 'b', 'c'];
console.log(myArray.indexOf('b')) // we get 1 (array's index starts from 0)

We can check the presence of an element in an array in couple of ways. The first way is by using the includes method and the second way is by using the indexOf method.

The includes method returns true if the value we have passed to this method exists in the array. The indexOf method returns the position of that value inside the array. If the value does not exist in the array, the include method returns false and the indexOf method returns -1.

example:

var myArray = ['a', 'b', 'c', 'd'];

console.log(myArray.includes('c'));  // we get true
console.log(myArray.indexOf('c')); // we get 2
console.log(myArray.includes('x')); // we get false
console.log(myArray.indexOf('x')); // we get -1

We can merge arrays in JavaScript by couple of methods. One is by using the concat method of an array. Another way is by using the spread operator (though we can do it in other different ways such as by using for loops, these two methods are more commonly used for simple merging requirements)

example:

let firstArray = ['a', 'b', 'c'];
let secondArray = ['x', 'y', 'z'];

let mergedArray = firstArray.concat(secondArray);
console.log(mergedArray); // we get [ 'a', 'b', 'c', 'x', 'y', 'z' ]

let secondMergedArray = [...firstArray, ...secondArray];
console.log(secondMergedArray); // we get [ 'a', 'b', 'c', 'x', 'y', 'z' ]

We can convert a JavaScript object to JSON string by using the stringify method of the built-in JSON object.

example:

let person = {
    id: 123,
    name: 'John Doe'
}

let jsonifiedString = JSON.stringify(person);
console.log(jsonifiedString); // we get {"id":123,"name":"John Doe"}

We can convert a JSON string to JavaScript object by using the parse method of the built-in JSON object.

example:

let jsonString = '{"id":123,"name":"John Doe"}';

let jsObject = JSON.parse(jsonString);

console.log(jsObject); // we get { id: 123, name: 'John Doe' }
console.log(jsObject.name); // we get John Doe

JavaScript have three types of functions. Named Functions, Anonymous Functions and IIFE ( Immediately Invoked Function Expression ). IIFE functions are also called as self invoked functions. Though they are technically anonymous functions, developers call them by a name.

example:

// Named Function

function myFunction() {
    console.log('Named Function');
}

myFunction();

// Anonymous Function

var x = function () {
    console.log('Anonymous Function');
}

x();

// IIFE - Immediately Invoked Function Expressions or Self Invoked Functions

(function () {
    console.log('IIFE Function');
}());