JavaScript Interview Questions And Answers

The list of top and best JavaScript interview questions and answers. This is generated on the behalf of real interviews questions of companies. Here is the list of some tricky JavaScript Interview Questions.

Q-1: What is ajax and it use parameter,methods and states?

Ans:
  • 0: request not initialized
  • 1: server connection established
  • 2: request received
  • 3: processing request
  • 4: request finished and response is ready

Q-2: What is difference between document.ready() and onload() function ?

if we alert something in both function then which will execute first ?

Ans:Ready

jQuery's document.ready()

jQuery's document.ready() event will be fire when the DOM is loaded, it's not wait for resources

We can write multiple document.ready() in a page but Body.Onload() event cannot.

onload()

The onload() event will be fire after the DOM and related content of the page got loaded like All Images,scripts etc

Q-3: What is the difference between JSON.stringify() and JSON.parse() ?

Ans:
  • Parse the data with JSON.parse(), and the data becomes a JavaScript object.
  • Convert a JavaScript object into a string with JSON.stringify().

Q-4: Difference between localStorage, sessionStorage, and cookies?

Ans:

Cookies

  1. The cookie is persistence, user can set expiration time to removed.
  2. Cookie work for old browsers.
  3. Data is sent back to the server on each HHTP request (HTML, JavaScript, CSS, Images, etc), It's increases the traffic between client and server.
  4. We can secured Cookies by setting the httpOnly flag as true for prevents client-side access.

localStorage

  1. localStorage persists until explicitly deleted, data are saved current and future visit of website.
  2. localStorage works for latest browsers.
  3. Shared data between all tabs and windows from the same-origin policy.
  4. Store the data 5-10 MB depend on browsers.

sessionStorage

  1. Data is available for current Tab only once Window or Tab is closed data is deleted.
  2. localStorage works for latest browsers.
  3. Data survives on page refresh, removed on closing the tab.it works on same-origin policy.
Details

Q-5: Difference between null and undefined in JavaScript?

Ans:

undefined in JavaScript

The undefined means a variable has been declared but has no value.

var nameTest;
console.log(nameTest); //undefined
console.log(typeof nameTest); //undefined

null in JavaScript

null is an assignment value,Variable declared and is an empty value

var nameTest=null;
console.log(nameTest); //null
console.log(typeof nameTest); //object