May 8, 2021

8 min read

Made Easy: JavaScript Interview Questions

What is javascript?

JavaScript is the Programming Language for the Web. It can update and change both HTML and CSS. It can calculate, manipulate and validate data.

JavaScript® (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it’s used in many non-browser environments as well. It is a prototype-based, multi-paradigm scripting language that is dynamic, and supports object-oriented, imperative, and functional programming styles.

How JavaScript code executes

JavaScript is a asynchronous (Moves to the next line only when the execution of the current line is completed) and single-threaded (Executes one command at a time in a specific order one after another serially) language. Total execution of a JavaScript program is completed within the event loop.

To know behind the scene of how JavaScript code gets executed internally, we have to know something called Execution Context and its role in the execution of JavaScript code.

Execution Context: Everything in JavaScript is wrapped inside Execution Context, which is an abstract concept (can be treated as a container) that holds the whole information about the environment within which the current JavaScript code is being executed.

Now, an Execution Context has two components and JavaScript code gets executed in two phases.

  • Memory Allocation Phase: In this phase, all the functions and variables of the JavaScript code get stored as a key-value pair inside the memory component of the execution context. In the case of a function, JavaScript copied the whole function into the memory block but in the case of variables, it assigns undefined as a placeholder.
  • Code Execution Phase: In this phase, the JavaScript code is executed one line at a time inside the Code Component (also known as the Thread of execution) of Execution Context.

The Call Stack keeps track of all the functions called in runtime. Heap is large block of mostly unstructured memory to store the objects. Call-back Queue is a list of messages to processed used by JS runtime. Messages in the queue are created whenever an event occurs and wait to be processed in the event loop. When the JS runtime starts processing the messages it takes the oldest one from the queue and removes it from the queue. Then, the call-back function associated with the message is placed onto the call stack and processed completely before any other message to be processed. The call-stack is cleared after processing one message and then another message is dequeued from the call-back queue for processing and goes on this way till there’s a single message in the queue. This cycle is called the event loop and this how JavaScript manages its events.

Key features of javascript


Variables are the most basic building blocks of JavaScript. A variable is a container for a value, like a number we might use in a sum, or a string that we might use as part of a sentence. Variables can contain just about anything, from strings and numbers to complex data, object and even entire functions. But one special thing about variables is that their contained values can change. Let’s look at a simple example:

var myName = 'Chris';
let myName = 'Bob';

We can use var or let to declare a variable. There are some differences between var and let(please check out the let reference page).

Numbers and Operators

Math is a fundamental part of life that we can’t get very far without. This is especially true when we are learning to program JavaScript (or any other language for that matter) — so much of what we do relies on processing numerical data, calculating new values, and so on, that you won’t be surprised to learn that JavaScript has a full-featured set of math functions available. All the math operations consist of numbers and operators, and we will know them in brief.

Types of numbers

  • Integers are whole numbers, e.g. 10, 400, or -5.
  • Floating point numbers (floats) have decimal points and decimal places, for example 12.5, and 56.7786543.
  • Doubles are a specific type of floating point number that have greater precision than standard floating point numbers.

We even have different types of number systems! Decimal is base 10 (meaning it uses 0–9 in each column), but we also have things like:

  • Binary — The lowest level language of computers; 0s and 1s.
  • Octal — Base 8, uses 0–7 in each column.
  • Hexadecimal — Base 16, uses 0–9 and then a–f in each column. You may have encountered these numbers before when setting colors in CSS.

JavaScript only has one data type for numbers, both integers and decimals and it is Number.

Arithmetic operators

Arithmetic operators are the basic operators that we use to do sums in JavaScript:

+ Addition Adds two numbers together. 6 + 9

- Subtraction Subtracts the right number from the left. 20 - 15

* Multiplication Multiplies two numbers together.3 * 7

/ Division Divides the left number by the right.10 / 5

% Remainder (sometimes called modulo) Returns the remainder left over after you’ve divided the left number into a number of integer portions equal to the right number.8 % 3 (returns 2, as three goes into 8 twice, leaving 2 left over).

** ExponentRaises a base number to the exponent power, that is, the base number multiplied by itself, exponent times. It was first Introduced in EcmaScript 2016.5 ** 2 (returns 25, which is the same as 5 * 5).

Assignment operators

Assignment operators are operators that assign a value to a variable. We have already used the most basic one, =, loads of times — it assigns the variable on the left the value stated on the right: x = 3. But there are some more complex types. The most common are listed below:

+= Addition assignment: Adds the value on the right to the variable value on the left, then returns the new variable valuex += 4; for x = x + 4;

-= Subtraction assignment: Subtracts the value on the right from the variable value on the left, and returns the new variable valuex -= 3; for x = x - 3;

*= Multiplication assignment: Multiplies the variable value on the left by the value on the right, and returns the new variable valuex *= 3; for x = x *3;

/= Division assignment: Divides the variable value on the left by the value on the right, and returns the new variable valuex /= 5;x = x / 5;

Comparison operators

=== Strict equality Tests whether the left and right values are identical to one another5 === 2 + 4

!== Strict-non-equality Tests whether the left and right values are not identical to one another5 !== 2 + 3

< Less than Tests whether the left value is smaller than the right one.10 < 6

> Greater than Tests whether the left value is greater than the right one.10 > 20

<= Less than or equal to Tests whether the left value is smaller than or equal to the right one.3 <= 2

>= Greater than or equal to Tests whether the left value is greater than or equal to the right one.5 >= 4


Strings are dealt with sim ilarly to numbers at first glance, but when you dig deeper you’ll start to see some notable differences.

let string = 'The revolution will not be televised.';

In Strings, we surround the value with quotes. We can use single quotes (‘ ’) or double quotes (“ ”) or backtick characters (` `).


Arrays are generally described as “list-like objects”; they are basically single objects that contain multiple values stored in a list. Array objects can be stored in variables and dealt with in much the same way as any other type of value, the difference being that we can access each value inside the list individually, and do super useful and efficient things with the list, like loop through it and do the same thing to every value.

let shopping = ['bread', 'milk', 'cheese', 'hummus', 'noodles'];

Accessing array items

// returns "bread"

Modifying array items

shopping[0] = 'tahini';
// shopping will now return [ "tahini", "milk", "cheese", "hummus", "noodles" ]

Some Basic Problem Solving

Find the largest element of an array

let arr = [33,12,93,34,54,89];let max = 0;for(let i=0; i<arr.length; i++){
if(arr[i] > max) max = arr[i];

Remove duplicate item from an array

let noDups = [];for(let i = 0; i < dups.length; i++){
if(noDups.indexOf(dups[i]) == -1)

Count the number of words in a string

let count=0;for(let i=0; i<str.length; i++){
if(str[i] == " " && str[i-1] != " "){

Reverse a string

let str = "He is good boy in his locality.";
let revStr = '';
// for(let i = str.length - 1; i >= 0 ; i--){
// revStr += str[i];
// }
// solution 1
// for(let i = 0; i < str.length; i++){
// revStr = str[i] + revStr
// }
// solution 2
// revStr = str.split('').reverse().join('');
// recursive
function reverseStr(str){
// if(str==='') return '';
// return reverseStr(str.substr(1)) + str.charAt(0);
// OR
return (str==='') ? '' : reverseStr(str.substr(1)) + str.charAt(0)

Calculate Factorial of a number using for loop

function factorial(num){
// with for loop
// let result = 1;
// if (num == 0) result =1;
// for(let i = 1; i<=num; i++){
// result = result*i;
// }
// return result;
// with while loop
let result = 1;
if (num == 0) result =1;
while(num > 0){
result *= num;
return result;

Calculate Factorial in a Recursive function

function factorial(num){
if(num == 0) return 1;
return num * factorial(num-1);

Create a Fibonacci Series using a for loop

function fibonacciSeries(n){
let fibArr = [0,1];
for(var i = 2; i <= n; i++){
fibArr[i] = fibArr[i-2] + fibArr[i-1];

Create Fibonacci series in a recursive way

function fibRecursion(n){
if(n == 0) return 0;
if(n == 1) return 1;
return fibRecursion(n-1) + fibRecursion(n-2);
function fibRecursionSeries(n){
if(n == 0) return [0];
else if(n == 1) return [0,1];
let fibo = fibRecursionSeries(n-1);
let nextElement = fibo[n-2] + fibo[n-1];
return fibo;

Check whether a number is a Prime Number or not

// take input from the user
const number = parseInt(prompt("Enter a positive number: "));
let isPrime = true;
// check if number is equal to 1
if (number === 1) {
console.log("1 is neither prime nor composite number.");
// check if number is greater than 1
else if (number > 1) {
// looping through 2 to number-1
for (let i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
if (isPrime) {
console.log(`${number} is a prime number`);
} else {
console.log(`${number} is a not prime number`);
// check if number is less than 1
else {
console.log("The number is not a prime number.");