Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 5 pages
Other

Functions in JavaScript: A Beginner’s Guide with Examples

Document preview thumbnail
Preview 2 out of 5 pages

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.

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

Document information

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

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-



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

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions