100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Functions in JavaScript: A Beginner’s Guide with Examples

Rating
-
Sold
-
Pages
5
Uploaded on
27-01-2025
Written in
2024/2025

This document introduces functions in JavaScript, explaining how to define and use them. It covers topics like parameters, return values, and arrow functions with practical examples to enhance your understanding.










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
January 27, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown

Subjects

Content preview

Functions in JavaScript
Functions are blocks of code designed to perform specific tasks. They allow
developers to reuse code, making programs more modular and efficient.

1. Defining Functions
Function Declaration

A named function that can be called before it is defined due to hoisting.

function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!

Function Expression

A function assigned to a variable. Unlike declarations, these are not hoisted.

const greet = function(name) {
return `Hello, ${name}!`;
};
console.log(greet("Bob")); // Output: Hello, Bob!

Arrow Function

A shorter syntax introduced in ES6.

const greet = (name) => `Hello, ${name}!`;
console.log(greet("Charlie")); // Output: Hello, Charlie!


2. Parameters and Arguments
Default Parameters

Provide default values for parameters if not supplied by the caller.

, function greet(name = "Guest") {
return `Hello, ${name}!`;
}
console.log(greet()); // Output: Hello, Guest!

Rest Parameters

Allow functions to accept an indefinite number of arguments as an array.

function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10

Spread Syntax

Spread elements of an array or object as arguments to a function.

const numbers = [1, 2, 3];
console.log(Math.max(...numbers)); // Output: 3


3. Function Scope and Closures
Scope

Functions in JavaScript have access to variables in their own scope, parent scope,
and global scope.

let globalVar = "I am global";
function example() {
let localVar = "I am local";
console.log(globalVar); // Access globalVar
}
example();
// console.log(localVar); // Error: localVar is not defined

Closures
$5.19
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Also available in package deal

Thumbnail
Package deal
Complete JavaScript Complete Study Bundle with Q&A & Exam Guide (16 documents)
-
16 2025
$ 93.04 More info

Get to know the seller

Seller avatar
rileyclover179 US
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
10 months
Number of followers
0
Documents
252
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions