Functions - Reading Assignment


#102
  1. We can create a function in JS with keyword function (in different ways), parameters (which may or may not be defined), body of function, calling out function,…
  2. A var is kind of a global binding while let is more local, not seen outside of the scope.
  3. Pure function is the “clear” function without side effects.

#103
  1. How can you create a function in Javascript?
    A function is created by using the function keyword
    in an expression.
  2. What is the difference between var and let in regards to scope?
    ‘let’ is local to the block in which it is declared, whereas
    ‘var’ is not thus limited and is visible within and/or
    without a particular function.
  3. What is a pure function?
    A pure function has no side effects and will return the same
    value when called with the same arguments.

#104

1. How can you create a function in Javascript?

Create a binding using a special keyword, “const”, to let the program know that it will be creating a function. From there, name the function and set it = to the keyword “function”. A function MUST contain regular quotes ‘()’ as well as curly brackets ‘{}’ that follow. The ‘()’ encloses the parameters (which are separated by commas) and the body of the function is written in the ‘{}’ (with each new line receiving a ‘;’ to separated them).

An example of a simple function construction can be found below:

const makeNoise = function() {
console.log(“Pling!”);
};

You can also create a function in a different way than with the “const” keyword which has slightly different underlying properties. This function creation method is called a “declaration” and is done by dropping the “const” keyword to instead just begin with the keyword "function" followed the “function value” (name of the function). In this form, the word “function” defines the binding name and points it at the function given. Doing this has the following differences to the method outlined above:

a) it is faster to declare than to the previous method
b) The function is not called based off it’s position in the code, but rather is moved to the top of it’s local scope so it can be passed by anything following it on the same level of scope (or any nested scopes contained within the functions’ local scope level). This gives the programmer the freedom from worrying about ordering functions to be available at the right time in the code.
c) No semi colon needed in first line of declaration (still needed in body)

Example below:

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

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

’var’- declares a binding that is always available to the global scope. There is no way to use it in a small local environment without worry of the larger program potentially changing it outside of the original block of code.

’let’- declares a binding that–if declared in a local block of code–is separate from the program at large and does not touch the global scope. So if the binding is created in a loop, the code both before and after the loop will be unable to “see” it. This is useful for creating blocks of code that are easily read even without knowing the larger context of the program.

**If there are multiple bindings of the same name, the block of code will look for the most locally named binding and use that for it’s purposes before beginning to search farther and farther outside of the block for more global bindings. “The local scope can see all the local scopes it contains” is what is known as ‘lexical scoping’.

3. What is a pure function?

A stand-alone function that when called:

a) doesn’t have any side effects - changes caused to global scope (or really anything that’s lasting)
b) Is not reliant on other side effects or bindings found in global scope in order to run. When given the same arguments it will ALWAYS produce the same output no matter what else is written in the program.

These types of functions are essentially self reliant. When called, they could be treated just the same as their output value because it’s unchanging after the value is created from the given arguments.


#105
  1. A function is introduced with the keyword function. The function can have parameters. and a body. The function body must be wrapped in braces.
  2. Bindings which are declared with the keyword let have a local scope and are just seen inside the block they are declared in. Bindings which are declared with the keyword var have a global scope and are seen in the whole program.
  3. A pure function does not have any side effects and does not rely on side effects from other code.

#106

How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function like so:

const makeNoise = function() {
  console.log("Pling!");
};

What is the difference between var and let in regards to scope?
Bindings declared with let and const are in fact 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.
Source: Eloquent JavaScript

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.
Source: Eloquent JavaScript


#107

Functions

1 How can you create a function in Javascript?
Functions have a set of parameters (in this case, only x) 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.

2 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.

3 What is a pure function?
A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope. Concretely, that means a pure function always returns the same result given same parameters. Its execution doesn’t depend on the state of the system. Pure functions are a pillar of functional programming.


#108

How can you create a function in Javascript?
use the keyword “function” followed by a parameter list (if needed) between round brackets then followed by a body wrapped in braces. The body can consist of 1 or more statements and a return keyword that returns the result back to the calling program.
What is the difference between var and let in regards to scope?
Var makes sure the variable is seen outside the block where it is used, while let only defines the variable inside the block and is not seen outside of that block
What is a pure function?
A pure function has no side effects and allways return the same result when passing the same parameters.


#109

1. How can you create a function in Javascript?
You can create a function in JavaScript by declaring it. You can do that by creating a simple binding with function as a value of that binding (const testFun = function()) or you can define it with the declaration notation (function testFun()) or arrow ( const testFun = () =>).

2. What is the difference between var and let in regards to scope?
var is scoped to the nearest function block and let is scoped to the nearest enclosing block. Both are global if outside any block.
Also, variables declared with let are not accessible before they are declared in their enclosing block.

3. What is a pure function?
The pure function is the one that does not have side effects and doesn’t rely on side effects of other piece of code in the program. This kind of function will behave in the deterministic way - when we will pass the same argument to that function it will give us the same result.


#110

Answers

  1. You can create a function by using the function keyword followed by its name and input parameters in parenthesis and the function body in braces.
  2. var will define a variable that is valid within the scope of the entire function, or global if created outside a block. let will define a variable that is only valid inside its enclosing block, statement, or expression on which it is used.
  3. Given the same input a pure function will always return the same output and it produces no side effects. One of the reasons to use pure functions is to avoid shared state.

#111
  1. A function is created with an expression that starts with the keyword function.

  2. Var is part of the global scope and let is local to the block it is created in (local scope).

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


#112
  1. 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.
  2. Bindings declared with let are local to the block that they are declared in whereas bindings declared with var, are visible throughout the whole function that they appear in as well as the global scope, if they are not in a function.
  3. Pure function is a function that does not create any side effects or rely on side effects.

#113
  1. How can you create a function in Javascript?

A function is created with the keyword “function”. It can have parameters and body which is inside curly brackets.

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

“Let” declare variables that are limited in scope to the block, statement of expression. “Var” defines a variable globally regardless of block scope.

  1. 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.


#114
  1. functions:

function a() {
   //body
}
a=function() {
   //body
}
a=()=> {
//body
}

var declares global variable
let declares local variables

  1. pure functions are completely self-contained, i.e. they neither read nor write to binding outside of their scope

#115

How can you create a function in Javascript?

There are different ways to create a function in Javascript. The main idea is define the name of the function, put the arguments and write commands in the body of the function.
Some examples how to create a function:
1- const name_of_function = function (arguments_here) {
functional codes are here
};
2- function name_ofthe_function(arguments_here){
functional codes are here
}
3- const name_of_function = (arguments_here) => { functional codes are here };

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

let is like local declaration. When you define a variable with let in the function, you cannot call the same variable outside of the function. The variable stays in that function.
var is creating more global variables. When you define a variable with var in the function, you also call the same variable outside of the function.
As a short summary, let is useful to create local variables, var is useful to create global variables.

What is a pure function?
The pure function is a function that never gives a side effects and give always same output when you use the same input.
If you are not sure that your function is pure function. Take the function and run on the different environments. If it gives the same output with the same inputs, it is pure function.


#116
  1. How can you create a function in Javascript?
    start with the function as a keyword, followed by a name, followed by parentheses ().

  2. What is the difference between var and let in regards to scope?
    let local scope (inside a function)
    var global scope (whole program)

  3. What is a pure function?
    is a value producing function with no side effects and has no reliance on other code with side effects.


#117
  1. How can you create a function in Javascript?
    Examples:

    let square = function(x) {
    return x * x
    };

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

  2. What is the difference between var and let in regards to scope?
    let means the binding is local to the block that it is declared in.
    var (pre-2015 JS) means the binding is visible throughout the whole function they’re in, or throughout the global scope, if they are not in a function.

  3. What is a pure function?
    It’s a value-producing function that not only doesn’t have any side effects, but also doesn’t rely on side effects from other functions to work.


#118

1st answer

function x(){
// body
}

2nd answer;

Var is always a global variable but let is local variable if it’s inside the scope.

3rd answer;

Pure function is a function that doesn’t have any side effects and always returns to the determined input values result.


#119

How can you create a function in Javascript?
Basically declare it and provide the syntax that it will need for accepting arguments and returning values

What is the difference between var and let in regards to scope?
var is global and let is local to the block it is in

What is a pure function?
One that does not have side effects and returns a value, one that when given the same inputs returns the same results each time it is run, one that doesn’t depend on global variables.


#120

How can you create a function in Javascript?
By creating an expression beginning with the keyword ‘function’ followed by () to accept (enclose) parameters
What is the difference between var and let in regards to scope?
‘var’ is accessible outside of the loop (much broader scope), whilst the ‘let’ variable is confined within the {} curly brackets of the ‘for loop’ (very limited scope)
What is a pure function?
A pure function only uses variables within its scope


#121

How can you create a function in Javascript?
A function definition is a regular binding where the value of the binding is a function.
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 (wrapped in braces).
A return statement determines the value the function returns.

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.
A binding created with the var keyword, is visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function.

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.
If a pure function works in one context it will work in any context.