Made Easy: JavaScript Interview Questions

Learn some commonly asked JavaScript interview questions

What is javascript?

How JavaScript code executes

Key features of javascript

Variables

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

Numbers and Operators

Types of numbers

Arithmetic operators

Assignment operators

Comparison operators

Strings

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

Arrays

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

Accessing array items

shopping[0];
// returns "bread"

Modifying array items

shopping[0] = 'tahini';
shopping;
// 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];
}
console.log(max);

Remove duplicate item from an array

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

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] != " "){
count++;
}
}
count++;
console.log(count);

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)
}
console.log(reverseStr(str));

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;
num--;
}
return result;
}
console.log(factorial(8));

Calculate Factorial in a Recursive function

function factorial(num){
if(num == 0) return 1;
return num * factorial(num-1);
}
console.log(factorial(8));

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];
}
console.log(fibArr);
}
fibonacciSeries(12);

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];
else{
let fibo = fibRecursionSeries(n-1);
let nextElement = fibo[n-2] + fibo[n-1];
fibo.push(nextElement);
return fibo;
}
}
console.log(fibRecursion(10));
console.log(fibRecursionSeries(10));

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;
break;
}
}
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.");
}

I am a Front-End Developer. I love to work with JavaScript, ReactJS, NodeJS, React Native and other front-end and/or JavaScript based technologies.