0% completed
Functions are one of the fundamental building blocks in JavaScript, enabling you to encapsulate a block of code and execute it wherever and whenever you want. Functions allow for code reuse, making your programs more modular, maintainable, and scalable. They can take parameters, perform actions based on those parameters, and return a value.
In JavaScript, functions can be defined in several ways, including function declarations, expressions, and ES6 arrow functions. Understanding how to define and invoke functions is crucial for any JavaScript developer.
Function Definition
A function in JavaScript is defined using the function
keyword, followed by a name, a list of parameters enclosed in parentheses ()
, and the function body enclosed in curly braces {}
.
Syntax
function
is the keyword that starts the function definition.functionName
is the name of the function.parameter1, parameter2, ...
are the function's parameters (optional).- The code block within
{}
is executed when the function is called.
Example: Defining a Simple Function
Let's define a simple function that displays a greeting message. This example will demonstrate the basic structure of a function without parameters and return statements.
function greet() { ... }
defines a function namedgreet
.- Inside the function,
console.log("Hello, world!");
logs a greeting message to the console. This demonstrates the basic structure and invocation of a function without parameters or a return statement.
Invocation of Function
Functions are called or invoked by using their name followed by parentheses. This process executes the code defined within the function.
Example: Invoking a Function
Here, we will call the function we defined earlier to display the greeting message.
greet();
invokes thegreet
function, which causes "Hello, world!" to be printed to the console.
Function Parameters
Parameters are named variables passed into a function. They allow functions to accept input and behave differently based on that input.
Example: A Function with Parameters
In this example, we will define a function that takes a name as a parameter and prints a personalized greeting message.
function greet(name) { ... }
declares a function namedgreet
that accepts one parameter:name
.- Within the function, the
console.log
statement constructs a greeting message that includes thename
parameter. This demonstrates how parameters can be used within a function to influence its behavior. greet("Alice");
calls thegreet
function with the argument"Alice"
, resulting in the console output:"Hello, Alice!"
. This shows how to pass data into a function when calling it.
Return Statement
Functions can return an output value to the caller using the return
statement.
Example: Returning a Value
Now, we'll create a function that calculates the square of a number and returns the result.
function square(number) { ... }
sets up a function calledsquare
that takes a single parameter:number
.return number * number;
calculates the square ofnumber
by multiplying it by itself and returns this value to the caller.var result = square(4);
calls thesquare
function with4
as the argument, and the return value (16) is stored in the variableresult
.console.log(result);
outputs the value ofresult
to the console, which is16
, showing the function's return value is successfully captured and displayed.
Function Expression
Function expressions allow you to assign a function to a variable.
Example: Function Expression
In this example, we will create a function expression that checks if a number is even and print the result.
var isEven = function(number) { ... };
defines a function expression namedisEven
. The function takes one parameter (number
) and returnstrue
if the number is even (i.e., divisible by 2 without a remainder) andfalse
otherwise.- The function is then called twice with different arguments (
4
and5
), demonstrating how it evaluates each number.console.log(isEven(4));
checks if4
is even, which it is, so it printstrue
. Similarly,console.log(isEven(5));
checks if5
is even, which it isn't, so it printsfalse
. - This example illustrates how function expressions can be used just like function declarations, but they also highlight the flexibility of assigning functions to variables.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible