Hey there! If you are attending an interview most of the questions will be asked from the JavaScript both theoretical and coding questions. In this article I am adding JavaScript coding interview questions and answers at one place which are frequently asked in interviews. This will help you to brush up your skills for the upcoming interviews.
Q1: Sort the numbers in ascending order in JavaScript
// ascending order
let numbers = [40, 100, 1, 5, 25, 10];
numbers.sort(function(a, b){return a-b});
console.log(numbers); // [ 1, 5, 10, 25, 40, 100 ]
// descending order
let numbers = [40, 100, 1, 5, 25, 10];
numbers.sort(function(a, b){return b-a});
console.log(numbers); // [ 100, 40, 25, 10, 5, 1 ]
Q2: Sort strings in JavaScript
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
console.log(fruits); // [ 'Apple', 'Banana', 'Mango', 'Orange' ]
Q3: Display the duplicate letters from the given string.
const stringValue = 'Sangareddy';
let stringValueOutput = stringValue.split('').filter((function(value, index, arr) {
return arr.indexOf(value) !== index;
})).join('');
console.log(stringValueOutput); // ad
Q4: How to swap two variables in JavaScript
// Destructuring assignment
let a = 1;
let b = 2;
[a, b] = [b, a];
a; // => 2
b; // => 1
console.log(a, b); // 2,1
Q5: Find the largest number in JavaScript
let a = 9, b = 4; c = 5;
let m1 = Math.max(a, b, c);
console.log(m1); //9
let m2 = Math.max(2, 5, 3);
console.log(m2); //5
// if it is an array
let ar1 = [1, 5, 99];
let m3 = Math.max(...ar1);
console.log(m3); //99
Q6: Write the fizzbuzz program in JavaScript
for(let i = 0; i< 10; i++){
let output = '';
if(i % 3 === 0) output += 'fizz';
if(i % 5 === 0) output += 'bazz';
console.log(output || i)
}
Q7: Reverse a string in JavaScript
let name = 'naveen kumar';
let newName = name.split('').reverse().join('');
console.log(newName); //ramuk neevan
// using for loop
let newArr = [];
for(var i = name.length; i>=0; i--){
newArr.push(name[i])
}
console.log(newArr.join('')); // ramuk neevan
Q8: Find the longest word in a string JavaScript
// Method1
function findLongestWord(str) {
let longestWord = str.split(' ').sort(function(a, b) {
return b.length - a.length;
});
return longestWord[0].length;
}
let output = findLongestWord("The quick brown fox jumped over the lazy dog");
console.log(output); //6
// Method2
function findLongestWord(str) {
let longestWord = str.split(' ').reduce(function(longest, currentWord) {
return currentWord.length > longest.length ? currentWord : longest;
}, "");
return longestWord.length;
}
let output = findLongestWord("The quick brown fox jumped over the lazy dog");
console.log(output); //6
Also Read Array Methods in JavaScript
Q9: Filter items based on specific letter
const countries = ['Norway', 'Sweden', 'Denmark', 'New Zealand'];
const startsWithN = countries.filter((country) => country.startsWith("N"));
console.log(startsWithN); // [ 'Norway', 'New Zealand' ]
Q10: Convert array to string JavaScript
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.toString();
console.log(text); // Banana,Orange,Apple,Mangoβ
Q11: Convert sentence to array JavaScript
let name = "Ganapathi Guptha";
let convertion = name.trim().split(" ");
console.log(convertion); //[ 'Ganapathi', 'Guptha' ]
// to convert each letter into array
let convertion = name.trim().split("");
Q12: Combine arrays and sort them in order JavaScript
let arr1 = [1,3,5,7]
let arr2 = [2,4,6,8,9]
let arr3 = [...arr1, ...arr2].sort((a, b) => a -b);
console.log(arr3) //[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Q13: Merge two arrays alternatively in JavaScript
// Method 1
let arr1 = [2, 6, 3, 1, 7];
let arr2 = [5, 4, 0, 8, 9, 11];
// Expected output = [2,5,6,4,3,0,1,8,7,9,11]
let output = [];
let arr= arr1.length > arr2.length ? arr1.length : arr2.length;
for (let i = 0; i < arr; i++) {
!isNaN(arr1[i]) && output.push(arr1[i]);
!isNaN(arr2[i]) && output.push(arr2[i]);
}
console.log(output); //[ 2, 5, 6, 4, 3, 0, 1, 8, 7, 9, 11 ]
// Method 2
let output = [];
let arr = arr1.length > arr2.length ? arr1.length: arr2.length;
for(let i = 0; i < arr; i++){
if(typeof arr1[i] === "number" ){
output.push(arr1[i])
}
if(typeof arr2[i] === "number"){
output.push(arr2[i])
}
}
console.log(output); //[ 2, 5, 6, 4, 3, 0, 1, 8, 7, 9, 11 ]
Q14: Remove falsy values from array JavaScript
let arr = [1,"", null,2, undefined, 3, false, 4, 'Naveen', true];
// remove all falsy values
let filterd = arr.filter(Boolean);
console.log(filterd); //[ 1, 2, 3, 4, 'Naveen', true ]
// remove all undefined values
let filterdUd = arr.filter((el) => el !== undefined);
console.log(filterdUd); //[ 1, '', null, 2, 3, false, 4, 'Naveen', true ]
Q15: Remove all holes from the given array
let arr2 = [0, , ,1, , , ,2, , ,3];
let filterHole = arr2.filter(() => true);
console.log(filterHole) //[ 0, 1, 2, 3 ]
Q16: Sort the array of objects in JavaScript
let names = [
{name: 'Naveen', age: 28, joinedDate: 'January 15, 2019'},
{name: 'Praveen', age: 27, joinedDate: 'December 15, 2017'},
{name: 'Ganapathi', age: 29, joinedDate: 'February 15, 2011'},
]
// filter by age
let orderAges = names.sort((a, b) => {
return a.age - b.age;
})
console.log(orderAges);
// filter by names
let orderNames = names.sort((a, b) => {
let a1 = a.name.toLocaleLowerCase();
b1 = b.name.toLocaleLowerCase();
if(a1 > b1){
return 1;
}
if(a1 < b1){
return -1;
}
return 0;
})
console.log(orderNames)
// filter by Dates
let orderDates = names.sort((a, b) => {
let d1 = new Date(a.joinedDate),
d2 = new Date(b.joinedDate);
return d1 - d2;
})
console.log(orderDates);
Q17: Add property to object JavaScript conditionally
In the below code there is an array of objects containing user information, add the isMinor property to the each object and set the value true if the age is less than 18 otherwise set false.
let names = [
{name: 'naveen', age: 29},
{name: 'praveen', age: 16},
{name: 'ganapathi', age: 30},]
let newNames = names.map((user) => {
return user.age < 18 ? {...user, isMinor: true} : {...user, isMinor: false}
})
console.log(newNames);
Q18: Sum of the numbers using Curry function
function add(x) {
return function(y) {
return function(z){
return x + y + z;
}
};
}
console.log(add(2)(3)(4)) // 9
Q19: How do you loop through or enumerate JavaScript object
let person = {
name: "Ganapthi",
age : 28,
role: "JS Developer"
};
for (let key in person) {
if (person.hasOwnProperty(key)) {
console.log(`${key}: ${person[key]}`);
}
}
// output: name: Ganapathi, age: 28, role: JS Developer
Q20: Remove the duplicate elements in array in JavaScript
In the below code there is an array containing repeated elements. remove all the duplicate elements and log the unique elements.
let fruites = ["apple", "banana", "cherry",
"mango", "banana", "cherry",];
//Method 1
function removeEl(arr){
return [...new Set(arr)]
}
console.log(removeEl(fruites));
//Output [ 'apple', 'banana', 'cherry', 'mango' ]
//Method 2
function removeEl(arr){
return arr.filter((el, index) => {
return arr.indexOf(el) === index;
})
}
console.log(removeEl(fruites));
//Output [ 'apple', 'banana', 'cherry', 'mango' ]
Q21: What is the output of below code snippets?
Code snippet:1
In the below code, there are 3 JavaScript statements, what is the order of execution?
console.log('First statement');
setTimeout(() => {
console.log('Second statement');
}, 0);
console.log('Third statement');
// Output:
// First statement
// Third statement
// Second statement
Thanks for reading JavaScript Coding Interview Questions and Answers, Hope you found this article helpful. If you are asked coding questions in JavaScript which do not exist in this article, do comment your question along with the answer so it will be helpful for others who are preparing for the interview.
11 comments
Comments are closed.