- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to call functions in JavaScript?
A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing the same code repeatedly. It helps programmers in writing modular codes.
The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.
Here’s an example −
<script> <!-- function sayHello() { alert("Hello there"); } //--> </script>
To call a function somewhere later in the script, you would simply need to write the name of that function as shown in the following code.
Example
Here’s an example showing how to call a function in JavaScript
<html> <head> <script> function sayHello() { d ocument.write ("Hello there!"); } </script> </head> <body> <p>Click the following button to call the function</p> <form> <input type = "button" onclick = "sayHello()" value = "Say Hello"> </form> <p>Use different text in write method and then try...</p> </body> </html>
- Related Articles
- How to call multiple JavaScript functions in onclick event?
- Invoking functions with call() and apply() in JavaScript
- How to call C++ functions from Java?
- How do I use strings to call functions/methods in Python?
- How to call a function in JavaScript?
- How to use setInterval function call in JavaScript?
- How to call a JavaScript function in HTML?
- How to delay a JavaScript function call using JavaScript?
- How to define nested functions in JavaScript?
- How to chain asynchronous functions in JavaScript?
- How to call JavaScript function in an alert box?
- How to call promise inside another promise in JavaScript?
- How to use named arguments in JavaScript functions?
- How to call jQuery function with JavaScript function?
- How to call a JavaScript function from C++?

Advertisements