

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 define a function in JavaScript?
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.
Syntax
Here’s the syntax −
<script> <!-- function functionname(parameter-list) { statements } //--> </script>
Try the following example. It defines a function called sayHello that takes no parameters,
<script> <!-- function sayHello() { alert("Hello World!"); } //--> </script>
- Related Questions & Answers
- How to define a JavaScript function using Function() Constructor?
- How to define global variable in a JavaScript function?
- How to define a JavaScript function using the expression?
- How do we use function literal to define a function in JavaScript?
- How to define functions inside a function body in JavaScript?
- How to define rest parameter for a function in JavaScript?
- How to define an Arrow Function in JavaScript?
- How to define custom sort function in JavaScript?
- How to define a function in Python?
- How to define a method in JavaScript?
- How to define a Regular Expression in JavaScript?
- How to define and call a function in Lua Programming?
- define() function in PHP
- How to define custom JavaScript exceptions?
- How to define nested functions in JavaScript?
Advertisements