Photo by Van Tay Media on Unsplash
JavaScript Interview Questions and Answer for Fresher and Developers.
This is Javascript interview questions and anwers for Fresher and javascript Lover ❤️
- What are the ways to create object in JavaScript ?
- There are many way to create object in javascript such as,
- Object constructor : const obj = new Object();
- using create method : const obj = Object.create(null);
- Object literal Syntax : const obj = { name: "Akshar", age:20, active: true }
function constructor : function Person(name){ this.name; }
What is JSON and its common operation :
- JSON stands for JavaScript Object Notation.
- JSON is text-based format, which is following object syntax.
It is useful when you want to transmit data across a network and it is basically just a text file with an extension .json Parsing JSON : JSON.parse(textOfJson); Stringify : JSON.stringify(obj);
Array Slice Method :
- The Slice() method return selected element in array as a new array. it selects element from starting element to ends point( optional ) of index element excluding last element.
for example :
let array = [1,2,3,4,5,6]; let slice1 = array.slice(0,2); //[1,2] let slice2 = array.slice(2,4); //[3,4] let slice3 = array.slice(2);//[3,4,5,6]
Array Splice Method :
- The Splice() method is used either add or removes items from an array and returned the removed items.
- The first argument describe the position of item for insertion or deletion and second argument describe number of element to be deleted.
- for example : ``` let array1 = [1,2,3,4,5,6]; let array2 = [1,2,3,4,5,6]; let array3 = [1,2,3,4,5,6];
let arr1 = array1.splice(0,2); // returns [1,2]; original array: [3,4,5] let arr2 = array2.splice(3); // returns [4,5]; original array : [1,2,3] let arr3 = array3.splice(3,1,"a","b"); // returns [4]; original array: [1,2,3,"a","b","c",5]
5. What is arrow function ?
- A arrow function is shorter syntax for function expression and does not have its own this, argument, super etc.
- These functions are best suited for non-method functions.
- for example :
const log = () =>{ console.log("Hello World!"); } log();
6. What is first-order function ?
- First-order function is a function that doesn't accept another function as an argument and doesn't return a function as its return value.
const firstOrderfunc = () => console.log("this is first order function!");
7. What is higher-order function ?
- Higher function is a function that accept another function as an argument or returns a function as a return value or both.
const firstOrderFunc = () =>{ console.log("this is first order function!"); }
const higherOrder = (returnFirstOrderFunc) => returnFirstOrderFunc(); higherOrder(firstOrderFunc);
8. What is unary function ?
- unary function is a function that accept one value exactly.
- for example :
const unaryFunc = (a) => console.log(a+10);
9. What is Currying Function ?
- Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with only a single argument.
- for example :
function multiply(a){ return function(b){ return function(c){ return abc; } } }
multiply(1)(2)(3); //6
10. Explain let keyword ?
- The let statement declares a block scope local variable. Hence the variables defined with let keyword are limited in scope to the block, statement, or expression on which it is used.
- usage of let :
let counter = 30; if(counter == 30){ let counter = 31; console.log(counter); //31 } console.log(counter); //30 because the variable in if block won't exist here
11. How do you redeclare variables in switch block without an error ?
- if you try to redeclare variable in switch block then it will cause error because there is only one block.
- for example (without block for case) :
let value = 1;
switch(value){ case 0: let name; break; case 1: let name; //SyntaxError for redeclaration break; }
- for example (with block for case):
let value = 1;
switch(value){ case 0:{ let name; break; } case 1:{ let name; //no error for redeclaration break; } }
12. What is IIFE ?
- IIIFE stands for Immediately invoked Function Expression.
- syntax :
(function(){ //Logic Here })();
- for example :
(function(){ var message = "This is IIFE"; console.log(message); })(); //IIFE
13. What is Hoisting ?
- Hoisting is a JavaScript Mechanism where variables, function declaration and classes are moved to top of their scope before code execution.
- for example [variable hoisting] :
console.log(message); //undefined var message = "Hoisting";
- in the same function, function declaration hoisted too :
console.log(message("Hello World!")); //Hello World!
function message(str){ console.log(str); }
14. classes in JavaScript ?
- In ES6, Javascript classes are primarily syntactic sugar over JavaScript’s existing prototype-based inheritance. For example, the prototype based inheritance written in function expression as below,
class bike{ constructor(name, color){ this.name = namel;; this.color = color; }
getInfo(){
return this.name + " " + this.color;
}
} ```