Skip to content

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Notifications You must be signed in to change notification settings

lassiecoder/100daysofjs

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 

Repository files navigation

Functions & Function Expressions

πŸ„ Functions

Functions in JavaScript are reusable blocks of code that perform a specific task. They allow you to group code into logical units, making your code more organized and easier to manage.

πŸ₯‘ Function Declaration

πŸ₯‘ Local variables

πŸ₯‘ Outer variables

πŸ₯‘ Parameters

πŸ₯‘ Default values

πŸ₯‘ Returning a value

πŸ₯‘ Naming a function

πŸ₯‘ Functions == Comments

πŸ„ Function Expresions

Functions expressions allow you to define a function as part of an expression, rather than as a standalone statement.

πŸ₯‘ Function is a value

πŸ₯‘ Callback functions

πŸ₯‘ Function Declaration and Function Expression


πŸ„ Functions

Function Declaration

It includes the function keyword, followed by the function's name, optional parameters within parentheses, and the function's body enclosed in curly braces.

Syntax:

function functionName(parameter1, parameter2, ...) {
  // function body
}

For example:

// Function declaration
function greet(name) {
  console.log('Hello, ' + name + '!');
}

// Calling the function
greet('John'); // Output: Hello, John!

Local variables

Variables declared within a function are local to that function and can only be accessed within its scope.

For example:

function greet(name) {
  let message = 'Hello, ' + name + '!';
  console.log(message);
}

greet('John'); // Output: Hello, John!
console.log(message); // Error: message is not defined

Outer variables

Functions can access variables declared outside of them. These variables are called outer variables.

For example:

let greeting = 'Hello';

function greet(name) {
  console.log(greeting + ', ' + name + '!');
}

greet('John'); // Output: Hello, John!

Parameters

Parameters are placeholders for values that are passed into a function when it is called. They allow functions to accept input and perform operations on it.

For example:

function add(a, b) {
  return a + b;
}

let sum = add(3, 5); // sum is 8

Default values

You can assign default values to function parameters. If a parameter is not provided when the function is called, it will use the default value.

For example:

function greet(name = 'Guest') {
  console.log('Hello, ' + name + '!');
}

greet(); // Output: Hello, Guest!
greet('John'); // Output: Hello, John!

Returning a value

Functions can return a value using the return statement. This allows the function to produce output that can be used elsewhere in your code.

For example:

function multiply(a, b) {
  return a * b;
}

let result = multiply(3, 5); // result is 15

Naming a function

Function names should be descriptive and indicative of their purpose. This makes your code more readable and easier to understand.

For example:

function calculateArea(radius) {
  return Math.PI * radius ** 2;
}

Functions == Comments

Well-named functions can often replace comments, providing clear and self-documenting code.

For example:

// Function to calculate the area of a circle
function calculateArea(radius) {
  return Math.PI * radius ** 2;
}

In the above example, the function name calculateArea conveys the purpose of the code better than the comment. Functions with descriptive names make the code more readable and maintainable.

πŸ„ Function Expressions

Function is a value

In JavaScript, functions are treated as values. This means they can be assigned to variables, passed as arguments, and returned from other functions just like any other data type.

For example:

function sayHi() {
  alert("Hello");
}

let func = sayHi; // Assigning the function to a variable

func(); // Outputs: Hello
sayHi(); // Outputs: Hello

In the above example;

  1. The function sayHi is declared and defined.
  2. The function is assigned to the variable func.
  3. Both func and sayHi can be called to execute the function and output "Hello".

This behavior illustrates that functions are indeed values in JavaScript. They can be stored in variables, passed around, and called just like any other value.

Callback functions

Callback functions are functions that are passed as arguments to other functions and are expected to be called back later if necessary.

For example:

function ask(question, yes, no) {
  if (confirm(question)) {
    yes();
  } else {
    no();
  }
}

function showOk() {
  alert("You agreed.");
}

function showCancel() {
  alert("You canceled the execution.");
}

ask("Do you agree?", showOk, showCancel);

In the above example, The function ask asks the question and depending on the user's response, yes() or no().

showOk and showCancel are callback functions. They are passed as arguments to the ask function and will be called back depending on the user's response.

We can also use function expressions to define callback functions inline:

For example:

ask(
  "Do you agree?",
  function() { alert("You agreed."); },
  function() { alert("You canceled the execution."); }
);

Anonymous function expressions are used directly inside the ask function call. They serve the same purpose as named functions but are declared inline and are not accessible outside of the ask function.

Function Declaration and Function Expression

Function Declaration and Function Expression are two ways to define functions in JavaScript, each with its own syntax and behavior.

Function Declaration:

Syntax: Declared as a separate statement in the main code flow using the function keyword.

Hoisted: Function declarations are hoisted, meaning they are moved to the top of their scope during compilation and can be called before they are defined.

For example:

function greet() {
  console.log('Hello!');
}
greet(); // Output: Hello!

Function Expression:

Syntax: Created inside an expression or another syntax construct by assigning it to a variable.

Not Hoisted: Function expressions are not hoisted, meaning they cannot be called before they are defined in the code.

For example:

let greet = function() {
  console.log('Hello!');
};
greet(); // Output: Hello!

Let's talk about differences!

Hoisting: Function declarations are hoisted, function expressions are not.

Accessibility: Function declarations are accessible throughout their scope, while function expressions are only accessible through the variable they are assigned to.

For example:

greet(); // Works fine with function declaration
function greet() {
  console.log('Hello!');
}

greet(); // Throws an error with function expression
let greet = function() {
  console.log('Hello!');
};

Use Cases:

Function declarations are useful for creating named functions that are accessible throughout the scope. Function expressions are useful for creating functions dynamically or passing them as arguments to other functions.

Keep coding and stay curious! 🌸 🎯 πŸ’πŸ»β€β™€οΈ

About

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published