Functions
Understand how to create and use functions in JavaScript.
Introduction
A JavaScript function is a block of code designed to perform a particular task.
There are two kinds of functions in JavaScript: built-in and developer created.
Some examples of built-in JavaScript functions include: concat()
, date()
, length()
etc.
A JavaScript function is executed when "something" invokes or calls it.
Calling a Function
The code inside a function will execute when something invokes or calls the function.
- When an event occurs (ex. when a user clicks a button)
- When it is called from JavaScript code
Function Example
Function Syntax
-
A JavaScript function is defined with the function keyword, followed by a name, then followed by parentheses ().
-
Function names can contain letters, digits, underscores, and dollar signs.
-
The parentheses may include parameter names separated by commas.
- (parameter1, parameter2, ....)
-
The code to be executed, by the function, is placed inside curly brackets .
-
Function parameters are listed inside the parentheses () in the function definition.
-
Function arguments are the values received by the function when it is called.
-
Inside the function, the arguments (parameters) behave as local variables.
-
Outputs of the operations done using parameters are sent back to the program using the return statement.
Function With Paramter and Return
Last updated on