1. What is JSON and its common operations?
JSON stands for JavaScript Object Notation.
It is a data representation format used for:
- Storing data (Client/Server)
- Exchanging data between Client and Server
It is just a text file with an extension of
Common JSON operations:
Parsing: It parses a JSON string and returns a corresponding value (object, etc.).
Example:
Stringification: It converts the given value into JSON string.
Example:
2. What is the syntax of a JSON object?
In JSON, all keys in an object must be enclosed with double-quotes. While in JS, this is not necessary.
So, keys must be strings, written with double quotes in JSON.
JS:
- JSON:
3. How do you access keys from JSON string?
First, parse the stringified JSON using the
Example:
4. Explain about async and await in JavaScript?
Async: The keyword
asyncbefore a function makes the function return a promise.Await: The keyword
awaitbefore a function makes the function wait for a promise.
The await keyword can only be used inside an async function.
5. Why do we use Async and Await?
The Async/Await is a modern way to consume promises. The
- We can avoid chaining promise altogether using async/await.
- It allows asynchronous execution while maintaining a regular, synchronous feel and readability.
6. Which one would you prefer between Promises or async/await?
The async/await simply gives you a synchronous feel to asynchronous code. It's a very elegant form of syntactical sugar and is more readable.
For simple queries and data manipulation, Promises can be preferred otherwise we will prefer async/await.
7. What is an Asynchronous Execution?
In Asynchronous execution, the second statement won't wait until the first statement execution.
Example: Fetch
Output:
8. What is a fetch method in JS?
The
Syntax:
- URL: URL of the resource
- OPTIONS: Request Configuration
Request Configuration:
We can configure a request by passing an options object with required properties like,
- Request Method
- Headers
- Body
- Credentials
- Cache, etc.
Example:
9. How to send the data to the server using the Fetch method?
We use the HTTP POST method to send the data to the server.
Example:
10. What are HTTP request methods?
HTTP Methods indicate the desired action to be performed for a given resource.
Name | Description |
---|---|
GET (Read) | Request for a resource(s) from the server |
POST (Create) | Submit data to the server |
PUT (Update) | The data within the request must be stored at the URL supplied, replacing any existing data |
DELETE (Delete) | Delete a resource(s) |
11. What is the difference between the HTTP GET method and the HTTP POST method?
The HTTP GET method is used to retrieve (get) data from a specified resource while the HTTP POST method is used to send data to the server.
12. What is meant by try...catch statement?
The
tryblock includes the code that might generate an error.The
catchincludes the code that is executed when an error occurs inside thetryblock.
Syntax:
13. Where do we use try...catch statements?
We use the try-catch statements when our code has a chance of throwing an exception which need to be handled.
14. Explain about SetInterval(), SetTimeout() ?
setTimeout() | setInterval() |
---|---|
setTimeout() allows us to run a function once after the interval of time. | setInterval() allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval. |
15. How to stop setTimeout()?
- By using clearTimeout()method we can stopsetTimeout().
- A call to setTimeoutreturns a timer identifiertimerIdthat we can use to cancel the execution.
Example:
16. Explain about setInterval and clearInterval methods in JavaScript?
setInterval():
The
Syntax:
- function: A callback function that is called repeatedly at the specified interval of time (delay).
- delay: time in milliseconds. (1 second = 1000 milliseconds)
clearInterval():
- The clearInterval()method cancels a schedule previously set up by callingsetInterval().
- To execute clearInterval()method, we need to pass the uniqueId returned bysetInterval()as an argument.
Syntax:
17. Can you explain the below code, and How it works?
Output:
Here,
The function in
18. What is the difference between Promises and Callback?
Promises | Callbacks |
---|---|
With promises, the executing function returns a special object to us and then we tell the promise what to do when the asynchronous task completes. | With callbacks, we tell the executing function what to do when the asynchronous task completes. |
19. What is a callback function?
A callback function is a function passed into another function as an argument.
Example:
20. What are the uses of callback?
Callbacks are generally used when the function needs to perform events before the callback is executed.
It will be useful when we need to use the result of the first function into another function.
For Example,
21. Explain about JS promises?
JS Promises:
- Promise is a way to handle Asynchronous operations.
A promise is an object that represents a result of an operation that will be returned at some point in the future.
A promise will be in any one of the three states:
- Pending: Neither fulfilled nor rejected
- Fulfilled: Operation completed successfully
- Rejected: Operation failed
Example:
- To know more, refer to JS Promises | Cheat Sheet
22. When to use JS Promises?
- Promises are used to handle asynchronous operations in JavaScript. They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events.
- To know more, refer to JS Promises | Cheat Sheet in JavaScript Essentials course
23. What are the handling methods of a Promise?
The methods
promise.then()
The
Example:
promise.catch()
The
Fetching a resource can be failed for various reasons like:
- The URL is spelt incorrectly
- Server is taking too long to respond
- Network failure error, etc.
Example: