FrontendDeveloper.in

JavaScript Interview Questions

Filter by topic:
  • Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.

  • A Service worker is basically a script (JavaScript file) that runs in the background, separate from a web page and provides features that don't need a web page or user interaction. Some of the major features of service workers are Rich offline experiences(offline first web application development), periodic background syncs, push notifications, intercept and handle network requests and programmatically managing a cache of responses.

  • The problem with service worker is that it gets terminated when not in use, and restarted when it's next needed, so you cannot rely on global state within a service worker's onfetch and onmessage handlers. In this case, service workers will have access to IndexedDB API in order to persist and reuse across restarts.

  • IndexedDB is a low-level API for client-side storage of larger amounts of structured data, including files/blobs. This API uses indexes to enable high-performance searches of this data.

  • Web storage is an API that provides a mechanism by which browsers can store key/value pairs locally within the user's browser, in a much more intuitive fashion than using cookies. The web storage provides two mechanisms for storing data on the client.

    1. Local storage: It stores data for current origin with no expiration date.
    2. Session storage: It stores data for one session and the data is lost when the browser tab is closed.
  • Post message is a method that enables cross-origin communication between Window objects.(i.e, between a page and a pop-up that it spawned, or between a page and an iframe embedded within it). Generally, scripts on different pages are allowed to access each other if and only if the pages follow same-origin policy(i.e, pages share the same protocol, port number, and host).

  • A cookie is a piece of data that is stored on your computer to be accessed by your browser. Cookies are saved as key/value pairs. For example, you can create a cookie named username as below,

    document.cookie = "username=John";
    

    Screenshot

  • Cookies are used to remember information about the user profile(such as username). It basically involves two steps,

    1. When a user visits a web page, the user profile can be stored in a cookie.
    2. Next time the user visits the page, the cookie remembers the user profile.
  • There are few below options available for a cookie,

    1. By default, the cookie is deleted when the browser is closed but you can change this behavior by setting expiry date (in UTC time).
    document.cookie = "username=John; expires=Sat, 8 Jun 2019 12:00:00 UTC";
    
    1. By default, the cookie belongs to a current page. But you can tell the browser what path the cookie belongs to using a path parameter.
    document.cookie = "username=John; path=/services";
    
  • You can delete a cookie by setting the expiry date as a passed date. You don't need to specify a cookie value in this case. For example, you can delete a username cookie in the current page as below.

    document.cookie =
    "username=; expires=Fri, 07 Jun 2019 00:00:00 UTC; path=/;";
    

    Note: You should define the cookie path option to ensure that you delete the right cookie. Some browsers doesn't allow to delete a cookie unless you specify a path parameter.

  • Below are some of the differences between cookie, local storage and session storage,

    FeatureCookieLocal storageSession storage
    Accessed on client or server sideBoth server-side & client-side. The set-cookie HTTP response header is used by server inorder to send it to user.client-side onlyclient-side only
    ExpiryManually configured using Expires optionForever until deleteduntil tab is closed
    SSL supportSupportedNot supportedNot supported
    Maximum data size4KB5 MB5MB
    Accessible fromAny windowAny windowSame tab
    Sent with requestsYesNoNo
  • The Window object implements the WindowLocalStorage and WindowSessionStorage objects which has localStorage(window.localStorage) and sessionStorage(window.sessionStorage) properties respectively. These properties create an instance of the Storage object, through which data items can be set, retrieved and removed for a specific domain and storage type (session or local). For example, you can read and write on local storage objects as below

    localStorage.setItem("logo", document.getElementById("logo").value);
    localStorage.getItem("logo");
    
  • The session storage provided methods for reading, writing and clearing the session data

    // Save data to sessionStorage
    sessionStorage.setItem("key", "value");
    
    // Get saved data from sessionStorage
    let data = sessionStorage.getItem("key");
    
    // Remove saved data from sessionStorage
    sessionStorage.removeItem("key");
    
    // Remove all saved data from sessionStorage
    sessionStorage.clear();