Functions - Reading Assignment

  1. You can use either const, which requires a semicolon at the end of the brackets, declare a function using “function name(){}” which does not require a semicolon at the end of the brackets, or you can use the arrow =>, which does require a semicolon at the end of the brackets.

  2. var defines a variable either globally or locally within an entire function, let variables are limited to the block, statement or expression in which it is used.

  3. A pure function is a function that does not rely on variables or calls outside of the function to completely perform the work of the function. Because of this, it will always return the same result given the same input.

  1. There are several ways in which functions can be created:
  • function expressions e.g const expressioFxn = function(arg1) { // function body };
  • function declaration that are not assigned a variable or binding
    e.g function declarationFxn(arg1) { // function body };
  • arrow functions which are the most shorthand way to write them
    e.g const arrowFxn = (arg1) => { // function body };

functions may or may not have arguments and may or may not return a value.

  1. When a ‘var’ variable is declared it within a function or loop, it can be accessed both inside and outside that scope. Whereas a ‘let’ variable can only be accessed within the scope that is declared but NOT outside of it. Additionally global variable defined with ‘var’ are added to global window object (if ‘strict’ mode not used) but variables with ‘let’ won’t be added to global window object.

  2. A pure function is one that produces the same result when passed the same set of arguments. In other words the result is not dependant on the result that was output from the previous time that the function was executed - which is known as a “side effect”.

  1. A function is created with an expression that starts with the keyword function and a set of parameters if needed.

Example:

function thisProgram(x) {
	//type code here
}
  1. In regards to scope between let and var:
  • The scope of let is set locally, so if the keyword is created inside of a loop, any code before or after it cannot see it. Same goes for the keyword const as well.
  • The scope of var is set globally, so any code can see it within the whole function, or throughout the global scope if its outside of a function.
  1. 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.
  1. A JavaScript function is a block of code designed to perform a particular task.
    The code to be executed, by the function, is placed inside curly brackets: {}
    function name ( parameter1, parameter2, parameter3 ) {
    // code to be executed
    }

  2. The main difference is the scope difference , while let can be only available inside the scope it’s declared, like in for loop, var can be accessed outside the loop for example. … This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

  3. Pure functions in Javascript are kind of Functions where the return value is determined by its parameter passed at call time. … If you provide some input parameter value to the function and it always returns the same result, then It’s Pure Functions in Javascript .

  1. You can create a function by defining a binding with a function keyword, using a parameter (or not), and defining the function’s body { how it should behave}.
  2. let (or const) are used to define bindings for local scopes and are not visible outside of them. Meanwhile, var lets to define a binding that is visible in the global scope (unless it is written in one of the local scopes).
  3. A pure function is a function that doesn’t create side effects and is not affected by side effects from other code. Also, when called a pure function always produces the same value if the same arguments are used.
  1. function Sum(a,b){return a+b;}
  2. The difference between them is that var is function scoped and let is block scoped.
  3. A pure function is a value producing function that doesn’t rely on the side effects from other code.

1.A javascript function is defined with the function word, followed by a name,followed by parentheses.
2.let are variables that are limited to a block, var is global, so it can be used throughout the whole function.
3.a pure function is a function that doesn’t have any side effects and is independent from side effects from another code. It always produces the same value if the same inputs are passed.

  1. There are 3 ways to create a function in JavaScript
  • As value
let sayHello = function()
{
  console.log("Hello");
}
  • With declaration notation
function square(x)
{
  return x * x;
}
console.log(square(4)); // 16
  • With arrow notation
const power = (base, exponent) => 
{
  let result = 1;
  for (let count = 0; count < exponent; count++) 
  {
    result *= base;
  }
  return result;
}

console.log('2^6 = ' + power(2, 6));
  1. let scope is limited by bloc whereas var scope is limited by function which means that var could be a global variable
  2. A pure function is a function without side effects

Reading Assignment - Functions

  1. How can you create a function in Javascript?
  2. function FunkyName (Argument1, Argument2){
       Useful Code
    }
    

  3. What is the difference between var and let in regards to scope?.
  4. let and const are used for bindings in local scope while var is used for global scope.

  5. What is a pure function?
  6. Pure functions are a specific type of value producing functions....p. 54

  1. A function is created with an expression that starts with the keyword: “Function”.
  2. The difference between them is the way each binding can be viewed in their respective scopes. “Let” is local, whilst “Var” is Global.
  3. A specific value-producing function that contains no side effects.
  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function.
  2. What is the difference between var and let in regards to scope?
    let is local to the block that it is declared in, var is visible throughout the whole function or throughout the global scope, if it is not in a function.
  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.
  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. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement. A function can have multiple parameters or no parameters at all.
  2. What is the difference between var and let in regards to scope? Bindings declared with let and const are local to the block that they are declared in, so if you create one of those inside of a loop, the code before and after the loop cannot “see” it. In pre-2015 JavaScript, only functions created new scopes, so old-style bindings, created with the var keyword, are visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function.
  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 function has the pleasant property that, when called with the same arguments, it always produces the same value (and doesn’t do anything else). A call to such a function can be substituted by its return value without changing the meaning of the code.
  1. How can you create a function in Javascript?
    -Create a function with a value/expression with the keyword “function” and a set of parameters and a function body.

  2. What is the difference between var and let in regards to scope?
    Var- Bindings created with this keyword can be visible throughout the whole function they appear in.
    Let-Bindings created with this keyword or “Const” are only local to the blocks they are declared inside.

  3. What is a pure function?
    A pure function is a function that accepts an input and returns a value without modifying any data outside its scope.

  1. A function is created with an expression that starts with the keyword function .
    Functions usually have a set of parameters and a body, which contains certain statements that are to be executed when the function is called. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement.
  2. The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.
  3. A pure function means that given the same input it’s always going to return the same output. A pure function produces no side effects. Pure functions are good at avoiding the bugs associated with shared state.