Published on

JavaScript Interview Questions

Authors

JavaScript is world most popular language for coding web applications, mobile application and desktop application.

Q1 - What are the Different Data Types in JavaScript and difference between Primitive Type and Non Primitive Type ?

Ans :-
  1. Primitive Type are immutable

    • Number
    • String
    • Boolean
    • undefined
    • null
    • symbol
  2. Non Primitive Type - are reference type and mutable

    • Objects
    • functions
    • array

Q2 - What's the Difference between var, let, const ?

Ans :-
  • var are globally scoped or function/locally scoped.
  • let is block scoped code bounded by {} and it can be updated but not re-declared
  • const is block scoped code bounded by {} and it cannot be updated but not re-declared

Q3 - What are the types of function in JavaScript ?

Ans :-

Function Declaration

function calculateTip(percentage, price) {
  return (percentage / 100) * price
}

Function Expression

const divideNumber = function (number, n) {
  return number / n
}

Anonymous Function

;(function () {
  console.log('I am an expression')
})()

Arrow Function

    const arrFunc => {
        return 'this is arrow method';
    }

Q4 - what is IIFE (Immediately Invoked Function Expression) is a JavaScript function.

Ans:- IIFE is JavaScript function that runs as soon as it is defined also called Anonymous Function
;(function () {
  console.log('IIFE')
})()

Q5- how create custom array method in JavaScript ?

Ans:- JavaScript is prototype based language . so we can define custom method in array class two multiply all elemts in array.
Array.prototype.multiple3 = function (ele) {
  let ab = []
  let array = this
  for (let a of array) {
    ab.push(a * 2)
  }
  return ab
}

let arr = ['2', '3', '4']
arr.multiple3()

Q6- what is Hoisting in javascript ?

Ans:- Hoisting is mechanism where variables and function declarations are lift to the top of their scope. In this example `myname` variable move up and hello method console return `hello john doe`.
function hello() {
  console.log('hello ' + myName)
}
hello()
var myName = 'john doe'

Q7 - what is closure in javascript ?

Ans:- Closure is a inner function have access to outher function variable and chain scope . closure have scope of inner variables , outer variables and globally
// global variable
var c = 10
function outerScope() {
  // outer variable
  var b = 10
  function innerScope() {
    // inner variable
    var a = 10
    console.log(a + b + c) // 30
  }
  return inner
}

Q8- difference between shallow copy and deep copy of object ?

Ans:-

Shallow copy just copy reference addresses of object.

var user = { name: 'john doe', age: 18, Profession: 'Software Developer' }

var userDuplicate = user //Shallow copy!

userDuplicate.name = 'mike'
console.log(user.name) // mike

Deep copy is copy object with its refrence addresses of object. spread oprater and Object.assign({}) only copy one level object . for Deep clone of nested object we use JSON.parse(JSON.stringfy({})) but it will not copy function in object , for deep clone we can use Lodash lib method _.cloneDeep({}) for Deep clone nested object with functions

var user = { name: 'john doe', age: 18, Profession: 'Software Developer' }
// For simple JSON objects deep copy
var newUser = JSON.parse(JSON.stringify(user))

//if you use jQuery, you can use:

// Shallow copy
var newUser = jQuery.extend({}, user)
// Deep copy
var newUser = jQuery.extend(true, {}, user)

// lodash Deep copy
var newUser = _.cloneDeep(user)