Functions, Objects and Arrays

Function declarations and arrow functions, scope and closures, objects, destructuring, spread, and the array methods you will use daily.

Est. 45 min Updated Suggest a fix
// Part 01 — Why functions

Give a Useful Operation a Name

A function packages a task behind a name. Instead of repeating the same calculation in several places, define it once, pass the values it needs, and return a result. A function declaration is available throughout its enclosing scope, which makes it convenient for named operations and introductory examples.

javascript
function calculateSubtotal(priceRupees, quantity) {
  return priceRupees * quantity
}

const subtotal = calculateSubtotal(250, 3)
console.log(subtotal) // 750

Parameters are the names in the definition; arguments are the values passed in. A function that returns a value lets its caller decide what to do next. A function that only logs output is harder to reuse in a test, a UI, or a different workflow. Keep input and output clear, and avoid functions that secretly depend on changing global state.

🎯 Pro Tip
Name functions with verbs such as calculateSubtotal, formatPrice, orfindLearner. A good name communicates intent without requiring a comment to explain every call.

Functions can have default parameters. Defaults are used when an argument is omitted or is explicitlyundefined, not when it is null. Validate inputs at boundaries, especially when values come from users or external services.

javascript
function greet(name = 'learner') {
  return 'Hello, ' + name
}

console.log(greet())          // Hello, learner
console.log(greet('Meera'))   // Hello, Meera
// Part 02 — Arrow functions

Choose a Function Form That Reads Clearly

Arrow functions are a concise function expression, often used for callbacks and small operations. They use=> between parameters and a body. When the body is one expression, braces and returncan be omitted. Add braces for multiple statements or when you want the function body to be especially explicit.

javascript
const formatLearner = (name) => 'Learner: ' + name
const addTaxEstimate = (amount, rate) => {
  const tax = amount * rate
  return amount + tax
}

console.log(formatLearner('Meera'))
console.log(addTaxEstimate(100, 0.18))

A key difference: arrow functions do not create their own this. They inherit it from the surrounding context. This makes them useful for callbacks that need the enclosing value, but object methods often use method syntax. Do not mechanically replace every function with an arrow; select the form that best communicates the role.

Function forms you will see in modern JavaScript
FormExampleTypical use
Declarationfunction total(items) { ... }Named reusable operation; supports hoisting within scope
Arrow expressionconst double = value => value * 2Callback or concise function value
Object methodconst cart = { add(item) { ... } }Behaviour associated with an object
💡 Note
An arrow function with an object literal needs parentheses around the object so JavaScript parses it as an expression:const makePoint = (x, y) => ({ x, y }).
// Part 03 — Scope

Understand Where Names Can Be Used

Scope determines where a variable name is visible. A block is the code inside braces. Variables declared withlet and const are block-scoped: code outside the block cannot use them. Prefer the narrowest useful scope, which limits accidental coupling and makes code easier to follow.

javascript
const course = 'JavaScript'

function describeCourse() {
  const level = 'Foundations'
  return course + ' — ' + level
}

console.log(describeCourse())
// level is not available here

A closure happens when a function retains access to variables from the scope where it was created, even after that outer function has returned. This is a normal language feature, not a special syntax. Closures support private state, callbacks, and functions configured with values.

javascript
function createCounter() {
  let count = 0
  return function increment() {
    count += 1
    return count
  }
}

const nextCount = createCounter()
console.log(nextCount()) // 1
console.log(nextCount()) // 2

The returned function still has access to its own count. A second call to createCounter()would create an independent count. Use closures intentionally: if a long-lived callback captures a large object, that object may remain reachable longer than expected.

📌 Real World Example
A formatter can close over a currency code or locale once, then return a reusable function for formatting several amounts. Keep configuration separate from the individual values being formatted.
// Part 04 — Objects

Represent a Related Record with an Object

Objects group related properties under one value. A learner record can hold an ID, a name, and a list of enrolled courses. Use dot notation when the property name is known in your code; bracket notation accepts a dynamic property name. Choose stable identifiers for records rather than relying on their position in an array.

javascript
const learner = {
  id: 'learner-104',
  name: 'Sana',
  city: 'Hyderabad',
  completedLessons: 4,
}

console.log(learner.name)
console.log(learner['city'])

const propertyToRead = 'completedLessons'
console.log(learner[propertyToRead])

Object property shorthand avoids repeating a variable name when the property and variable are the same. Destructuring pulls selected properties into local variables. It can make a function signature clearer, but do not destructure so much that the source of a value becomes hard to find.

javascript
const name = 'Sana'
const city = 'Hyderabad'
const profile = { name, city }

const { name: learnerName, completedLessons = 0 } = {
  name: 'Sana',
}
console.log(learnerName, completedLessons)

Object values can contain nested objects or arrays. When reading optional nested data from an API, use optional chaining only when absence is expected, and supply a sensible fallback where the UI needs one. Do not use optional chaining to hide a broken data contract that should instead be validated.

javascript
const enrollment = { learner: { name: 'Sana' } }
const displayName = enrollment.learner?.name ?? 'Unknown learner'
console.log(displayName)
// Part 05 — Arrays

Store Ordered Collections and Transform Them

An array stores an ordered sequence. Indexes start at zero, and length reports the number of items. Use for...of when you need to process every value with statements. Daily application code often uses array methods to describe a transformation, which can avoid manual index bookkeeping.

javascript
const modules = ['Fundamentals', 'Objects', 'DOM']
console.log(modules[0])       // Fundamentals
console.log(modules.length)   // 3

for (const moduleName of modules) {
  console.log('Module:', moduleName)
}

map returns a new array with one transformed value for each input item. filter returns a new array containing only items that satisfy a test. Neither method changes the original array when your callback simply returns a value; keeping transformations pure makes behaviour easier to predict.

javascript
const lessons = [
  { title: 'Values', minutes: 12, complete: true },
  { title: 'Functions', minutes: 18, complete: false },
  { title: 'Arrays', minutes: 15, complete: true },
]

const titles = lessons.map((lesson) => lesson.title)
const completed = lessons.filter((lesson) => lesson.complete)
console.log(titles)
console.log(completed)

reduce combines an array into one result, such as a total. Pass an initial accumulator value so empty arrays are handled safely and the result type is predictable. For simple transformations, prefer mapor filter; an overly clever reduce callback is difficult to maintain.

javascript
const totalMinutes = lessons.reduce(
  (total, lesson) => total + lesson.minutes,
  0,
)
console.log(totalMinutes) // 45
⚠️ Important
sort() mutates its array, and its default ordering is string-based. When numeric ordering matters, provide a comparator and sort a copy if the original order is meaningful: [...scores].sort((a, b) => a - b).
// Part 06 — Copying

Use Spread and Destructuring for Clear Updates

The spread syntax (...) expands an iterable into values or copies enumerable object properties. It is useful for making a shallow copy and adding or overriding fields. A shallow copy does not recursively copy nested objects; references inside the copy still point to the same nested values.

javascript
const originalTags = ['beginner', 'web']
const tagsForCourse = [...originalTags, 'javascript']

const learner = { name: 'Sana', city: 'Hyderabad' }
const updatedLearner = { ...learner, city: 'Pune' }

console.log(tagsForCourse)
console.log(updatedLearner)
console.log(learner.city) // Hyderabad

Array destructuring is convenient for positions; object destructuring is convenient for named fields. Rest syntax collects remaining values. Use it when the group has a clear meaning, not just to shorten a line.

javascript
const coordinates = [17.4, 78.5, 'Hyderabad']
const [latitude, longitude, city] = coordinates

const { name, ...publicDetails } = {
  name: 'Sana',
  email: 'sana@example.test',
  city: 'Pune',
}
console.log(latitude, longitude, city)
console.log(publicDetails)

Updating an array immutably often means building a new array with spread, map, orfilter. This avoids surprising other code that holds the old reference. In a user interface, predictable references make change detection and testing easier.

javascript
const lessons = ['Values', 'Functions']
const withDom = [...lessons, 'The DOM']
const withoutValues = lessons.filter((title) => title !== 'Values')

console.log(withDom)
console.log(withoutValues)
console.log(lessons) // unchanged
// Part 07 — Classes

Recognise Class Syntax, but Keep the Model Simple

Classes provide syntax for constructing objects with related data and methods. They may be useful when several instances share the same behaviour. JavaScript classes use prototypes under the hood; you do not need to start every data model as a class. A plain object and functions are often simpler for application records.

javascript
class Course {
  constructor(title, lessonCount) {
    this.title = title
    this.lessonCount = lessonCount
  }

  summary() {
    return this.title + ' has ' + this.lessonCount + ' lessons'
  }
}

const course = new Course('JavaScript', 6)
console.log(course.summary())

The constructor initializes an instance. Methods describe behaviour that belongs with instances. Consider whether a class adds real clarity: a standalone formatCourse(course) function may be easier to test than creating a class hierarchy for a few records.

🎯 Pro Tip
Prefer composition and small functions over deep inheritance trees. The goal is understandable behaviour, not using every language feature in each project.
// Part 08 — Pitfalls

Common Mistakes

Forgetting return in a multi-line arrow function

Why: Braces create a function body where a return statement is required.

Fix: Write return explicitly, or use a single expression without braces.

Mutating an array while iterating over it

Why: Removing an item shifts later indexes and can skip values.

Fix: Use filter to build a new array, or iterate carefully over a copy.

Assuming spread makes a deep copy

Why: Nested objects are copied by reference in a shallow spread.

Fix: Copy nested values intentionally or use an appropriate data-model strategy.

Using reduce for every array operation

Why: A compressed accumulator can hide whether you are filtering, mapping, or totaling.

Fix: Choose map, filter, or a loop when it explains the task more directly.

// Part 09 — Practice

Build a Small Course Catalogue Model

Practice

Transform course data using functions and arrays

  1. Write a function that accepts a price in rupees and quantity and returns a subtotal.
  2. Represent three lessons as objects with title, minutes, and complete fields.
  3. Use map to get lesson titles and filter to find incomplete lessons.
  4. Use reduce with an initial value to calculate the total lesson minutes.
  5. Create a learner object, destructure its name, then create an updated copy with spread without changing the original.
  6. Write a class with a constructor and one method, then describe one reason a plain object might be enough instead.

Hint: Keep each transformation readable, and test it with an empty array as well as a non-empty one.

🎯 Key Takeaways

  • ✓Functions name reusable operations; parameters carry inputs and return values make results easy to reuse.
  • ✓Arrow functions are concise callbacks and inherit this; method syntax is often clearer for object methods.
  • ✓Lexical scope limits where bindings are visible, while closures retain access to their creation scope.
  • ✓Objects represent named fields; arrays represent ordered collections.
  • ✓Destructuring extracts values, and spread makes shallow copies with clear updates.
  • ✓map transforms, filter selects, and reduce combines; provide an initial reduce value.
  • ✓Classes are useful syntax for shared instance behaviour, but simple objects and functions often suffice.

Next

What comes next

Block and function scope, hoisting, closures in practice, and how this is decided in methods, callbacks, arrow functions and classes.

Lesson 03 · Scope, Closures and this →

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...