Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Functions in JavaScript

A function in JavaScript is a set of statements that perform specific computations based on input parameters and produce output. Functions can be defined using the 'function' keyword, and they can be declared, expressed, or invoked in various ways. The document provides syntax examples and explains the difference between function parameters and arguments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Functions in JavaScript

A function in JavaScript is a set of statements that perform specific computations based on input parameters and produce output. Functions can be defined using the 'function' keyword, and they can be declared, expressed, or invoked in various ways. The document provides syntax examples and explains the difference between function parameters and arguments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Functions in JavaScript

A function is a set of statements that take inputs, do some specific


computation, and produce output. The idea is to put some commonly or
repeatedly done tasks together and make a function so that instead of writing
the same code again and again for different inputs, we can call that function.

JavaScript Function Syntax


A JavaScript function is defined with the function keyword, followed by
a name, followed by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs
(same rules as variables).

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 name(parameter1, parameter2, parameter3) {


// code to be executed
}

Function parameters are listed inside the parentheses () in the function


definition.

Function arguments are the values received by the function when it is


invoked.

Inside the function, the arguments (the parameters) behave as local


variables.

Example 2: This example shows a basic declaration of a function in


javascript.
 JavaScript

function calcAddition(number1, number2) {

return number1 + number2;


}

console.log(calcAddition(6,9));

Output
15
In the above example, we have created a function named calcAddition,
 This function accepts two numbers as parameters and returns the
addition of these two numbers.
 Accessing the function with just the function name without () will return the
function object instead of the function result.
There are three ways of writing a function in JavaScript:
Function Declaration: It declares a function with a function keyword. The
function declaration must have a function name.
Syntax:
function geeksforGeeks(paramA, paramB) {
// Set of statements
}
Function Expression: It is similar to a function declaration without the
function name. Function expressions can be stored in a variable
assignment.
Syntax:
let geeksforGeeks= function(paramA, paramB) {
// Set of statements
}

Function Invocation
The code inside the function will execute when "something" invokes (calls)
the function:

 When an event occurs (when a user clicks a button)


 When it is invoked (called) from JavaScript code
 Automatically (self invoked)

You might also like