JS Function Invocation šŸŽÆ

beginner
22 min

JS Function Invocation šŸŽÆ

Welcome to the exciting world of JavaScript (JS) Function Invocation! In this lesson, we'll explore how to create, call, and understand functions – a fundamental concept in programming that allows us to reuse code and make our lives easier. šŸŽ‰

Let's start with the basics.

What is a Function? šŸ“

A function is a block of code designed to perform a specific task. It encapsulates code to increase modularity, readability, and reusability.

In JavaScript, you can define a function using the function keyword followed by the function name, parentheses (), and curly braces {}.

javascript
function greet(name) { console.log("Hello, " + name + "!"); }

In this example, we've created a function called greet that takes one argument, name. When called, it will display a personalized greeting.

Function Invocation šŸ’”

To call a function, we need to invoke it. There are two ways to invoke a function in JavaScript:

  1. Function Call with Parentheses
    • Calling a function with parentheses means we're asking the function to execute its code.
javascript
greet("Alice"); // Output: Hello, Alice!
  1. Function Call with the new keyword
    • Using the new keyword with a function creates a new object instance and invokes the function as a constructor. This will automatically call the function and execute its code.
javascript
const alice = new greet("Alice"); // Output: Hello, Alice! (However, the function actually returns undefined)

šŸ’” Pro Tip: When using the new keyword, the function must return an object or be a constructor function. If the function doesn't return anything explicitly, it will implicitly return undefined.

Function Arguments šŸ“

Functions can take arguments, which are values passed to the function during its invocation. These values can then be used within the function's code.

javascript
function calculateArea(width, height) { const area = width * height; console.log("The area is: " + area); } calculateArea(4, 5); // Output: The area is: 20

Function Return Values šŸ’”

Functions can also return values, which are the output of the function. The returned value can be assigned to a variable or used in other expressions.

javascript
function addNumbers(a, b) { const sum = a + b; return sum; } const result = addNumbers(3, 5); console.log("The sum is: " + result); // Output: The sum is: 8

Function Scope šŸ“

In JavaScript, variables have a scope – a defined area within which they can be accessed. Function scope refers to the variables that are accessible within a function.

javascript
function myFunction() { let x = 10; console.log(x); // Output: 10 (because x is in the function scope) } myFunction(); console.log(x); // Output: ReferenceError: x is not defined (because x is not in the global scope)

Function Callback šŸ’”

A callback function is a function passed as an argument to another function, which is then executed inside the outer function. This allows for the reuse and modularization of code.

javascript
function greetCallback(callback) { const name = "Alice"; callback(name); } function sayHello(name) { console.log("Hello, " + name + "!"); } greetCallback(sayHello); // Output: Hello, Alice!

Practice Time šŸŽÆ

Let's test your understanding!

Quick Quiz
Question 1 of 1

What is the output of the following code?

Quick Quiz
Question 1 of 1

What is the output of the following code?