Functions - Reading Assignment

  1. You can create a function by typing the keyword function followed by the name of your function.

  2. var declaration is global, while let only works locally within the function from which it is declared

  3. A function that does not produce any side effects but only returns values determined by its inputs.

1 Like
  1. You can create it with a word function (a) or you can make an arrow function (b).
    a: function hiThere() {console.log(“Hi There”);}
    b: hiThere = () => console.log(“Hi There!”);

  2. "var" makes variable global or it makes is local to an entire function. “let” makes scope limited to the block.

  3. Pure function is a value-producing function that does not produce side effects and does not rely on other functions that produce side effects.

  1. How can you create a function in Javascript?
    Declare it using the ‘function’ reserved word.
  2. What is the difference between var and let in regards to scope?
    Scope. Var is global, Let tied to a block of code
  3. What is a pure function?
    A value producing function that has no side effects and does not rely on side effects from other code.

1. How can you create a function in Javascript?
There are three way to create a Javascript function.

Binding Function:

let sum = function (a, b) {
    return a + b;
}

Declaration Function:

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

Arrow Function:

let sum = (a, b) => a + b;

2. What is the difference between var and let in regards to scope?
Bindings declared with let are visible just in block that they are declared in. On the other hand, bindings declared with var are visible throughout the entire function or the global scope if they are not in a function.

3. What is a pure function?
Is a function that return some value, don’t have side effects and don’t need any other information unless the parameters.

1. How can you create a function in JavaScript ?
A function is created with the keyword function, it has a set of parameters and a body which contains the statements to be executed when the function is called.
var funcitonName = function (parameters){
_____body
}
2. What is the difference between var and let in regards to scope?
The scope of var is bigger than let, it is global. For example in the case of a var binding, it can define a global **variable otherwise a let binding only exists in the block where is defined, it is local. **
3. What is a pure function?
It is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code.

  1. To create a function in JS, you need the keyword function, set of parameters, and a body.

    //example
     const cube = function(x) {
         return x * x * x;
    };
    
  2. The terms let and const are local variables with respect to the function scope.
    The term var may be defined as a global or local variable, regardless of scope.

  3. A Pure Function doesn’t have any dependencies outside its scope. So giving it the same parameters must yield to the same outcome every time.

  1. In Javascript you create a function by making a statement where the binding inside is composed by the word “function” and followed by round braces
    example: const f = function (x){return x*x; }
  2. Parameters and bindings declared by let are local and not visible from the outside. While parameters and bindigs declared by var end up in the nearest functionscope or in the global scope.
  3. A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code.
  1. function name (x,y) {
    return x * y;
    }
  2. Var is global and recognized by whole program. Let is local to that block only
  3. A function that relies on no other code and produces value
How can you create a function in Javascript?
    function switchSquare(square) {
              if (square == "#"){
                square = " ";
              } else {
                square = "#";
              }
              return square;
            }
What is the difference between var and let in regards to scope?
let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. 

What is a pure function?
A pure function, when called with the same arguments, always produces the same value and doesn’t do anything else. It has no side effects and doesn’t rely on side effects from other code — for example, it doesn’t read global bindings whose value might change.
  1. How can you create a function in Javascript?
    Create an expression that begins with the keyword function. The function will have 0 or more arguments and a body containing statements to be executed. If a value is to be returned to the program calling the function then there will be a ‘return’ statement with the value to be returned.
    Note: arguemnts are placed in parentheses just after the keyword ‘function’ and the body of the function is placed inside curly brackets.

  2. What is the difference between var and let in regards to scope?
    Bindings declared with let are in fact local to the block that they are declared in whereas bindings declared with var in the main program are global in scope and bindings created with var in functions apply throughout the whole function.

  3. What is a pure function?
    A pure function is a value producing function that has no side effects and doesn’t rely on side effects from other code. When called with the same arguments, a pure function always produces the same value

function functionName(parameter1,…)
{
code to be executed
}
2. Var - global scope unless defined in a function, let - local to the block they are declared in
3. Pure function is a function that doesn’t return any side effects and does not rely on side effects from other code

  1. Function consist of:
  • name of the function
  • parameters - inside the parentheses
  • body of the function - consist of statements to be executed when is the function called, is inside of curly braces
  1. The ‘let’ bindings are local to the block, in which are they called.
    The ‘var’ bindings are visible globaly - through the whole function, in which are they called.

  2. Pure function’s return value is determined only it’s input values, without observable side effects.

1. How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function . Functions have a set of parameters and a body, which contains the statements that are to be executed when the function is called eg
const square = function(z) {
return z * z;
};
console.log(square(10)); // → 100

2. What is the difference between var and let in regards to scope?
A key aspect in understanding functions is understanding scopes. Eachblock creates a new scope.
Parameters and bindings declared in a given scope are local and not visible from the outside. Bindings declared with var behave differently—they end up in the nearest function scope or the global scope

3. What is a pure function?
A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code—for example, it doesn’t read global bindings whose value might change. A pure functionhasthepleasantpropertythat, whencalledwiththesame arguments, it always produces the same value (and doesn’t do anything else).

How can you create a function in Javascript?
Begins with the keyword function. It can have no or multiple arguments. Body of the function is the statement that will be called once it’s executed.

What is the difference between var and let in regards to scope?
let are local to where they are declared and var is global.

What is a pure function?
A pure function is a specific kind of value-producing function that not only
has no side effects but also doesn’t rely on side effects from other code

Functions Reading Assignment

  • Q.1 How can you create a function in Javascript?
    • A function is created by binding a name equal to function, followed without a space, by curved brackets (or braces), which may contain zero or more parameter names. When calling, or using the function any value given will become the parameters given to the function and will therefore help determine the result of the function. Lastly, add the code that the function will use, between two curly brackets “{ }”.
  • Q.2 What is the difference between var and let in regards to scope?
    • If you declare a binding using the “let” keyword it is local to the block of code that it is inside, for example, inside a loop.
      The loop itself can see a binding immediately outside it. The binding has different values further outside the scope, that is, in nested loops, it can only see the innermost one.
    • If you use “var” to declare a binding (as used in any pre-2015 JavaScript), it will be visible throughout the function, or throughout the entire global scope if not inside a function.
    • Using “const” to declare a binding follows the same rules as the “let” keyword.

The following code provides an example of the visibility of JavaScript bindings. “z” is visible outside the loop, but not “y”.

    if (true) {
      let y = 20;
      var z = 30;
      console.log(x + y + z);
      // → 60
    }
    // y is not visible here
    console.log(x + z);
    // → 40

The following code shows that the first time the “halve” function was called,even though it changes “its own n” to 50, the following output showed n equal to 10, in its outer scope.

    const halve = function(n) {
      return n / 2;
    };
    let n = 10;
    console.log(halve(100));
    // → 50
    console.log(n);
    // → 10
  • Q.3 What is a pure function?
    • A pure function only produces a value and does not produce any side-effects and does not rely on the side effects of any other code or the value of a global bindings, which might have changeable values.
    • The benefit of a pure function is that if you call that it with any given parameters it will always produce the same result.
  1. How can you create a function in Javascript?

There are several ways of creating a function in Javascript. The common features of these methods revolve around:

  • binding the function to its name and give its arguments e.g. function timesTwo(x);
  • then following the declaration put the code block representing what the function is doing within curly brackets e.g.

function timesTwo(x) {
return 2 * x;
}

  1. What is the difference between var and let in regards to scope?

var defines global variables while let declares variables whose scope is limited to the block in which they are declared.

  1. What is a pure function?

A pure function does not have ay side effect.

How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function. Functions have a set of parameters and a body, which contains statements that are to be executed when the function is called.
What is the difference between var and let in regards to scope?
let is local to the block in which it is declared, inside of a loop. var is global if not in a function or visible throughout the whole function they appear in.
What is a pure function?
A pure function is a specific kind of value-producing function that not only
has no side effects but also doesn’t rely on side effects from other code.

  1. Function is created by using a keyword function().
  2. By using a var, variable can be created for local as well global access and let is used only for the local access.
  3. A pure function does not have any side effects and does not depend on side effects from other codes whose values can change.

A) const square = function(x) {
return x * x;
};

B) const square1 = (x) => { return x * x; };
OR
const square2 = x => x * x;

C) function square(x) {
return x * x;
}

  1. let = local ; var = global

  2. pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code.
    A pure function has the pleasant property that, when called with the same arguments,
    it always produces the same value (and doesn’t do anything else)

@Guactoshi, thank you for that link to the Eric Elliott video on pure functions! It helped me understand it better, and also some of the reasons we would be needing them.