Functions - Reading Assignment

#242
  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.

0 Likes

#243
  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”.

0 Likes

#244
  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.
0 Likes

#245
  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 .

0 Likes