Video Lesson

Lesson Notes

Introduction to JavaScript Functions (Simple)

Functions let you encapsulate reusable code so you don’t repeat yourself.

Defining a Function

Use the function keyword, give it a name, and specify parameters:

1function add(numberOne, numberTwo) { 2 console.log(numberOne + numberTwo); 3}
  • add is the function name.
  • numberOne and numberTwo are parameters (local to the function).

Calling (Executing) the Function

Invoke the function by its name and pass arguments in parentheses:

1add(1, 2); // Logs: 3 2add(45, 2); // Logs: 47 3add(3, 4); // Logs: 7
  • Arguments are positional: the first value goes to numberOne, the second to numberTwo.

Function Scope

  • Variables declared as parameters or inside the function exist only within the function’s {} block.
  • Trying to access them outside will cause an error:
    1console.log(numberOne); // ReferenceError: numberOne is not defined

Why Use Functions?

  • Abstraction: Hide complex logic behind a simple name.
  • Reusability: Write code once, call it many times.
  • Maintainability: Update logic in one place.
1// Instead of repeating: console.log(3 + 5); 2// You write: 3add(3, 5); // Logs: 8

Next Steps

In future videos, we’ll explore return values, arrow functions, and more advanced patterns!

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Basic functions

Unlock the power of JavaScript by mastering functions in this engaging lesson! You'll learn how to define, call, and leverage functions for code reusability, maintainability, and abstraction, setting a strong foundation for your programming journey.

4m 40s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed