Accordion
JavaScript Q&A
Reference: https://github.com/sudheerj/javascript-interview-questions
- Parsing: Transforming a JSON-formatted string into a native JavaScript object. JSON.parse(jsonString);
- Stringification: Converting a JavaScript object into a JSON-formatted string, commonly used for data transmission or storage. JSON.stringify(object);
The slice() method in JavaScript is used to extract a section of an array, returning a new array
containing the selected elements.
The slice() method does not mutate (change) the original array; instead, it returns a new array
containing the extracted elements.
The splice() method in JavaScript is used to add, remove, or replace elements within an array.
Unlike slice(), which creates a shallow copy and does not alter the original array, splice()
modifies the original array in place and returns an array containing the removed elements.
A 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. i.e, It's a regular function that works with primitive or
non-function values.
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.
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; local storage and session
storage.
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";
Cookies are used to remember information about the user profile(such as username). It basically
involves two steps,
- When a user visits a web page, the user profile can be stored in a cookie.
- Next time the user visits the page, the cookie remembers the user profile.
Promises are used to handle asynchronous operations, especially in languages like JavaScript, which
often work with non-blocking operations such as network requests, file I/O, and timers. When an
operation is asynchronous, it doesn't immediately return a result; instead, it works in the
background and provides the result later. Handling this in a clean, organized way can be difficult
without a structured approach.
Promises are used to:
- Handle asynchronous operations.
- Provide a cleaner alternative to callbacks.
- Avoid callback hell.
- Make code more readable and maintainable.
Promise.all is a promise that takes an array of promises as an input (an iterable), and it gets
resolved when all the promises get resolved or any one of them gets rejected.