[2021] Top 10 Javascript Advanced & Programming Interview Questions and Answers
1. Explain the difference between Object.freeze(), Object.seal() and Const ?
2. Explain prototype in Javascript with example (s).
3. What is the difference between ES5 and ES6 and what needs to programmatically changed ?
4. Difference between function declaration and function ?
5. What is Arrow function ?
6. Can we able to use the typeof to check the array ?
No, typeof “[]” also returns “object” only.
So to check whether the given value is array or not need to use Array.isArray()
var input = [1 , 2, 3]; if(typeof(input) == 'object'){ console.log("this is object"); } // where as the below prints tru Array.isArray(input); // prints true as input is array.
7. How do you divide the given array into 2 parts in javascript ?
Lets consider the given size is of length even, then
var array = [1, 2, 3, 4, 5, 6]; var length = array.length; var array2= array.splice(length/2, length); var array1= array.splice(0, length/2); console.log(array1); // (3) [1, 2, 3] console.log(array2); // (3) [4, 5, 6]
If your given array is odd length then,
var array = [1, 2, 3, 4, 5, 6,7]; var length = array.length; var array2= array.splice(length/2, length); var array1= array.splice(0, length/2); console.log(array1); // (3) [1, 2, 3] console.log(array2); // (3) [4, 5, 6, 7]
8. How do you divide the array into given size equally in javascript ?
var array = [1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15]; var splitSize = 3; var arraySize = array.length; var temp = 0; for(let i=0;i < arraySize;i++){ temp++; if(splitSize == temp){ temp =0; var newArray = array.splice(0, splitSize); console.log(newArray); } } // prints [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, 11, 12]
We have another way as well
let inputArray = [1,2,3,4,5,6,7,8,9]; let splitCount = 3; let outputArray = []; for (let i=0;i<inputArray.length;i+=splitCount){ let tempArray = []; for(let j=i;j<splitCount+i;j++){ tempArray.push(inputArray[j]); } outputArray.push(tempArray); } console.log(outputArray);