Stay-Hack06 / js-functions-returns-expressions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Return Statements and Function Expressions in JavaScript

Return Statements

  • In JavaScript, the return statement is used in functions to specify the value that the function should return when it is called. When a return statement is executed in a function, it immediately stops the function's execution and sends the specified value back to the code that called the function.
  • The return statement is crucial in functions, as it allows you to provide output or results from the function's internal calculations to the rest of your code. Without the return statement, a function would not send any information back to the calling code, and the result of the function's execution would be undefined.
function functionName(parameters) {
 // Function body - code that does some calculations or tasks
 return valueToReturn;
}

Function Expressions

  • In JavaScript, a function expression is a way to define a function as part of an expression, typically by assigning it to a variable. It's an alternative syntax for creating functions compared to the more common function declaration.
    • Function expressions are powerful because they allow you to create anonymous functions (functions without names) and pass them as arguments to other functions, store them in variables, or return them as values from other functions. This makes function expressions a fundamental part of functional programming and enables more advanced coding patterns.
 const functionName = function(parameters) {
 // Function body - code that performs some tasks
};

Prep

  1. Connect your js file to your HTML file
  2. Open your code in the browser and open your console

Activities

  1. In the js file, complete the list of exercises.

About


Languages

Language:JavaScript 94.8%Language:HTML 5.2%