0% found this document useful (0 votes)
3 views

JS FUNCTIONS

The document provides an overview of JavaScript functions, including their definitions, syntax, and different types such as function declarations, expressions, and arrow functions. It explains function parameters, including default and rest parameters, as well as the arguments object and the concept of passing values by reference or value. Additionally, it covers nested functions and their ability to access variables from outer functions, emphasizing modular code design.

Uploaded by

BENAZIR AE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JS FUNCTIONS

The document provides an overview of JavaScript functions, including their definitions, syntax, and different types such as function declarations, expressions, and arrow functions. It explains function parameters, including default and rest parameters, as well as the arguments object and the concept of passing values by reference or value. Additionally, it covers nested functions and their ability to access variables from outer functions, emphasizing modular code design.

Uploaded by

BENAZIR AE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JS FUNCTIONS

Functions in JavaScript are reusable blocks of code designed to


perform specific tasks. You define a function once, and it can be
called or invoked multiple times within your program, improving
efficiency and reducing code redundancy.
Function Syntax:
There are different ways to define a function in JavaScript. Here’s the
basic syntax:
Function Declaration:
- The keyword function is used.
- A function name is followed by parentheses that contain the
function parameters (optional).
- The function body is enclosed in curly braces {}.
Example:

Function Expression:
A function can also be stored in a variable, which is known as a
function expression. This function may or may not have a name.
Example:
Function Parameters:
In JavaScript, function parameters act as placeholders for values that
the function can accept when it’s called.
JavaScript Function Parameters
1. Default Parameters
Default parameters allow you to set initial values for named
parameters if no value or undefined is passed when the function is
called.
Syntax:

Example:
2. Rest Parameters
Rest parameters allow a function to accept an indefinite number of
arguments, which are grouped into an array.
3. Arguments Object
The arguments object is an array-like object accessible within all non-
arrow functions. It contains the values of the arguments passed to
the function.
Example:
4. Pass by Value
In JavaScript, when you pass a primitive value (like numbers or
strings) to a function, the function receives a copy of that value.
Changing the parameter inside the function does not affect the
original value outside the function.
Example:

5. Objects Passed by Reference


When passing objects as arguments, the function receives a
reference to the object, not a copy. This means any changes made to
the object inside the function affect the object outside as well.
Arrow Function
Arrow functions were introduced in ES6 as a shorter and more
concise way to declare anonymous functions. They provide an
alternative to the traditional function keyword and simplify the
syntax, making code more readable. In addition to the shorter syntax,
arrow functions also bind this lexically, which is particularly useful in
callback functions or within object methods.
Syntax of Arrow Functions
Arrow functions use the following syntax:
1. Arrow Function Without Parameters
When no parameters are required, an arrow function is defined with
empty parentheses ():

2. Arrow Function with a Single Parameter


If there is only one parameter, parentheses can be omitted:
Example:

3. Arrow Function with Multiple Parameters


With multiple parameters, parentheses are required:
Example:
4. Arrow Function with Default Parameters
Arrow functions can have default parameter values:
Example:

5. Returning Object Literals


To return an object literal from an arrow function, wrap the object in
parentheses:
Nested Function
In JavaScript, a nested function refers to a function defined inside
another function. The inner function has access to the variables,
parameters, and scope of the outer function, creating a scope chain.
This allows for encapsulation and modular code design, where the
inner function can be used to perform operations related to the
outer function. This is useful for closures, where inner functions
maintain access to the outer function’s scope even after the outer
function has completed execution.
Syntax of Nested Functions
Example 1: Basic Nested Function
In this example, the innerFunction has access to the variable
outerVar from the outerFunction.

Example 2: Nested Function with Parameters


This example demonstrates how the inner function can use the
parameters of the outer function.

You might also like