JavaScript Interview Questions and Answer for Fresher and Developers.

This is Javascript interview questions and anwers for Fresher and javascript Lover ❤️

  1. What are the ways to create object in JavaScript ?
  2. There are many way to create object in javascript such as,
  3. Object constructor : const obj = new Object();
  4. using create method : const obj = Object.create(null);
  5. Object literal Syntax : const obj = { name: "Akshar", age:20, active: true }
  6. function constructor : function Person(name){ this.name; }

  7. What is JSON and its common operation :

  8. JSON stands for JavaScript Object Notation.
  9. JSON is text-based format, which is following object syntax.
  10. 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);

  11. Array Slice Method :

  12. 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.
  13. 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]
    
  14. Array Splice Method :

  15. The Splice() method is used either add or removes items from an array and returned the removed items.
  16. The first argument describe the position of item for insertion or deletion and second argument describe number of element to be deleted.
  17. 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;
}

} ```

Did you find this article valuable?

Support Javascript Blogs by becoming a sponsor. Any amount is appreciated!