Blogs

Home

Differences between Local storage and Session storage?

LocalStorage: This read-only interface property provides access to the Document's local storage object, the stored data is stored across browser sessions. If we set any data in localStorage it will be stored there unless we delete it. If we close tab, even close the browser localStorage data always stored. In short, localStorage sets data permanently.

SessionStorage: Session Storage objects can be accessed using the sessionStorage read-only property. The difference between sessionStorage and localStorage is that localStorage data does not expire, whereas sessionStorage data is cleared when the page session ends. In short, sessionStorage sets data temporarily.

Differences between Global scope and local scope variable?

Global scope: Global scope contains all of the things defined outside of all code blocks. A code block simply consists of grouped statements inside curly braces ({ }). if statement, loops, function are examples of structure that create a code block. A global variable has global scope. A global variable is accessible from anywhere in the code.

Local scope: Local scope contains things defined inside code blocks. A local variable has local scope. A local variable is only accessible where it's declared.

How eventLoop works in JavaScript?

JavaScript is single threaded but can works asynchronously because of the event loop. The event loop is a process that waits for the Call Stack to be clear before pushing callbacks from the Task Queue to the Call Stack. Once the Stack is clear, the event loop triggers and checks the Task Queue for available callbacks. If there are any, it pushes it to the Call Stack, waits for the Call Stack to be clear again, and repeats the same process.

Event loop=> call stack =><= event queue => event loop

How many ways we can get undefined?

There are 8 ways to get undefined

  • variable that is not initialized will give undefined.
  • function with no return
  • function parameter that is not passed will be undefined
  • if return has nothing on the right side will return undefined.
  • property that doesn't exists on an object will give you undefined.
  • accessing array elements outside of the index range
  • deleting an element inside an array
  • set a value directly to undefined.