Dino Geek, try to help you

How to use callbacks in JavaScript?


Callbacks are a key feature in JavaScript and allow you to run a function after another function has finished execution, hence the term “call back”.

To understand this, let’s first define a simple function:

```
function greeting(name) { alert(‘Hello ‘ + name);
}
```

This function will just simply print out `Hello ` followed by whatever the `name` parameter is.

Now, let’s create another function that takes two parameters:

- a value `name`
- a function `callback`

```
function processUserInput(callback) { var name = prompt(‘Please enter your name.’); callback(name);
}
```

In this function, `callback` is being invoked right after `name` variable is assigned a prompt value. Part of the beauty of JavaScript is that it’s an interpretive language, which means callback can be any valid function that’s been defined:

```
processUserInput(greeting);
```

In this case, `greeting` function is a callback function. After you input your name, the script should alert ‘Hello, your_inputted_name’.

The advantage of using callback function is that they make sure a function doesn’t run before the first one is finished. So callbacks ensure correct order of execution. They are useful when you need to perform something after a certain task is done.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use