Skip to content

Commit 3f92d0b

Browse files
committed
2 parents a46cbc1 + 0c91957 commit 3f92d0b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,72 @@ if (result) {
321321
**Prompt**
322322
Displays a message to the user and also takes input from the user. It returns the input value to the user.
323323

324+
## Callbacks
325+
326+
Callbacks are JavaScript's way of implementing asynchronous programming. A callback is a function that is passed as an argument to another function and is executed after its parent function has been completed.
327+
328+
javascript
329+
function callback() {
330+
console.log("Callback function");
331+
}
332+
333+
function parentFunction(callback) {
334+
console.log("Parent function");
335+
callback();
336+
}
337+
338+
339+
If the callback is an anonymous function, then we can also directly call the function at the time of parent call.
340+
341+
javascript
342+
function parentFunction(callback) {
343+
console.log("Parent function");
344+
callback();
345+
}
346+
347+
parentFunction(function() {
348+
console.log("Callback function");
349+
});
350+
351+
352+
## Arrow functions
353+
354+
These are a type of functions that are used to write the functions in a short way. They are anonymous functions, with a special syntax.
355+
356+
It is named an arrow function, because of the arrow symbol `=>` in the syntax.
357+
358+
javascript
359+
var functionName = (param1, param2, ...) => {
360+
// function body
361+
return value;
362+
}
363+
364+
365+
If the function has only one statement, then we can remove the curly braces and the return keyword.
366+
367+
javascript
368+
var functionName = (param1, param2, ...) => statement;
369+
370+
371+
If the function has only one parameter, then we can remove the parenthesis.
372+
373+
javascript
374+
var functionName = param1 => {
375+
// function body
376+
return value;
377+
}
378+
379+
380+
They are generally used with JS's native functions like `map`, `filter`, `reduce`, etc. When we need to apply the same operation to a set of values.
381+
382+
## Promises
383+
384+
These are the basics of asynchronous programming in JS. A promise is an object that may produce a single value some time in the future. Either a resolved value or a reason that it's not resolved.
385+
386+
javascript
387+
388+
389+
324390
```javascript
325391
var name = prompt("Enter your name");
326392

0 commit comments

Comments
 (0)