Functions - Reading Assignment

  1. We create a function "with an expression that starts with the keyword function". It consists of a set of parameters separated by a comma or only one argument — enclosed in brackets; “and a body, which contains the statements that are to be executed when the function is called”.

  2. A binding (or variable) declared with let is local to the block it is declared in. Old-style bindings (from pre-2015 JavaScript) created with var “are visible throughout the whole function they appear in — or throughout the global scope, if they are not in a function”.

  3. A pure function is a specific function that returns a value, has no side effects nor relies on side effects from other code.

1 Like
  1. How can you create a function in Javascript?
    ⁃ a function is created by using the keyword “function”. Allowing you to call it whenever deemed necessary. In code it would be written as const function name = function(“values you want to evaluate” or “can be left open”) {what you want the function to do}.

  2. What is the difference between var and let in regards to scope?
    ⁃ the let binding allows you to declare variables that are local , which are limited to the statement/block that they were used in. Where as var makes a variable global, which can be used just in both the entirety of the block of code or just locally.

  3. What is a pure function?
    ⁃ a pure function is a function that has no side affects. It produces a given value and that value never changes.

1 Like

How can you create a function in JavaScript? Function binding(paramenter){ body;}; i.e

function square(x) {

return x * x;

}

What is the difference between var and let regarding scope? Var is global in scope (can be used throughout the program). Let is local in scope (can be seen only within the function). Same holds true if var is a function.

What is a pure function? A function when given the same argument will always produce the same value. It does not rely on -or- have side-effects.

1 Like
  1. How can you create a function in Javascript?

You wrap a piece of program in value. It give us a way to structure larger programs, to reduce repetition, to associate names with subprograms and to isolate these subprograms from each other.

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

Let is 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.
What is a pure function?

Var is visible throughout the whole function that they appear it - or throughout the global scope, if they are not in a function.

  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—for example, it doesn’t read global bindings whose value might change. A pure function has the property that, when called with the same arguments, it always produces the same value (and doesn’t do anything else).

1 Like
  1. function funtionName (Var1, Var2, …) {set of instructions}
  2. let are variables that are limited to a block (for, if, white, etc), var can be globally
  3. A function that that has nothing to do with side effect… Same arguments, always return the same result.
1 Like
  1. How can you create a function in Javascript?
    A function is created with an expression stat starts with the keyword function. Functions have a set of parameters and a body, which contains the statements tat are to be executed when the function is called. The function body of a function created this way must always be wrapped in curly braces.

  2. What is the difference between var and let in regards to scope?
    Var and let are both used for variable declaration in Javascript but the difference between them is that var is function scoped and let is block scoped.

  3. What is 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. A pure function when called with the same arguements, it always produces the same value.

1 Like
  1. How can you create a function in Javascript?
    to declare a function you must use the “function” keyword followed by the parameters and the body of the functions

  2. What is the difference between var and let in regards to scope?
    let will only allow the program to “see” the value of the binding in the block it was created on
    var scope will depend on wher it’s declared, in a function or globally

  3. What is a pure function?
    a function that does not rely on other funtions or bindings to perform its task, given the same argument they will always produce the same value and in turn are easy to verify

1 Like
  1. How can you create a function in Javascript?

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

  1. What is the difference between var and let in regards to scope? Var is ‘hoisted’ to the top of its block, regardless of where it was declared. Meaning, if it is called for as an argument in line 3, but is declared in line 4, it will be recognised. Let only exists from where it is declared, and wont be recognised if called before.

  2. What is a pure function? A pure function has no side-effects, or ‘outside’ influences or requests. ie, it does not call upon an outside, or global var value, within its function block. Note that a pure function can call another pure function, and this is not considered a side-effect.

1 Like

How can you create a function in Javascript?
function functionName(parameters) {code to execute}

What is the difference between var and let in regards to scope?
Bindings declared with let and const are local, the code before and after the loop cannot “see” it.
Bindings created with the var keyword are visible throughout the whole function that they appear in - or throughout the the global scope, if they are not in a function.

What is a pure function?
A pure function is a function where the return value is only determined by it’s input values, without observable side effects.

1 Like
  1. How can you create a function in Javascript?

Function in JavaScript can be created in 3 ways:

a) By regular binding where the value of the binding is a function. In this case function expression starts with the keyword “function”.

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

b) if function binding is created with the “function” keyword used at the start of a statement.

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

c) Instead of the “function” keyword we can use an arrow (=>)
const square1 = (x) => { return x * x; };
or
const square2 = x => x * x;
All functions have a set of parameters and a body, which contains the statements that are to be executed when the function is called.

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

Bindings declared with var are visible throughout the whole function that they appear in while bindings declared by let are local and not visible from the outside if for example used in subfunction or inside the loop. Both bindings declared by let or var, if they are not inside function or loop, are visible throughout the global scope.

  1. What is a pure function?

A pure function is a specific kind of function that produces value, has no side effects and doesn’t rely on side effects from other parts of code such as global bindings. A pure function will always produces the same value when called with the same arguments.

1 Like
  1. By defining with keyword function, followed by () containing zero or many parameters and a body function with braces.

  2. “let” is local, therefore only seen within the block wheres are “var” is accessible throughout the program.

  3. A pure function is a value producing function without side effects and does not rely on side effects from other code.

1 Like
  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?
    Bindings declared with let 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.
    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.

1 Like
  1. A function is created with an expression that starts with the keyword function. A function will have set of parameters and body. Body contains statement or statements that are to be executed when they are called.

  2. ‘let’ allows you to declare variabes that are limited to the scope of the block whereas ‘var’ allows you declare variable globally or locally.

  3. A function that doesn’t modify variables outside of it’s scope and returns the same result given the same parameters.

1 Like
  1. How can you create a function in Javascript?

You can create a function all by itself in Javascript by typing in the word “function” and defining it in the statement and giving it a binding in the local environment to call upon to use:

const average = function (a,b,c); {

return (a+b+c)/2;

};

Console.log (average (23,12,45));

Alternatively, it can be done by omitting the word “function”, adding an equals sign, and on the right hand side of this, a pointing outwards arrow. The body can be defined all in parentheses or all out of parentheses, however in a similar format:

const average = a,b,c => (a+b+c)/2;

OR

const average = (a,b,c) => {return (a+b+c)/2}

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

Bindings defined with “const” and “let” are only seen within that particular block whereas “var” has further scope (ie it can be seen and used outside of the local block being created).

  1. What is a pure function?

Pure functions will always output the same result; meaning there are no variables with the code that can affect this consistency (an obvious example would be using “var” to define the value of a binding outside of the definition; by its very nature it is variable). This result will only depend on the code’s input arguments.

Also, a pure function will not produce any direct ‘side effect’ from the definition such as pop up boxes, data mutation or printing to a screen.

1 Like
  1. You would make a function like this:
const square = function(x) {
  return x * x;
};

console.log(square(12));
// → 144
  1. If you use let than it will be only acceseble from inside of the block. However if you use var than it will be acceseble outside of the block.

  2. A pure function is a function that allwase returns the exact same thing every single time.

1 Like
  1. Functions are created with keyword function. For example:
function square (x){
return x * x;
}
  1. When we use var inside the function it is visible in whole program environment and when we use let it is visible only inside of function environment.
  2. Pure function returns only a value without side effects.
1 Like
  1. How can you create a function in Javascript?
    function Name (List of parameters if any) {
    Code to be executed (Body)
    }
    For Example:
    function Multiplyer (a, b){
    return a*b;
    }

  2. What is the difference between var and let in regards to scope?
    let defines a variable which is visible in the block or function it is defined in whereas var defines a variable visible globally through out the code or locally within the scope of a function if defined within said function.

  3. What is a pure function?
    A pure functions return is the result of the provided parameters and has no side effects. They are easy to test and to be reused.

1 Like

To declare with a variable and make it equal to a function.
Ex. const func = function() { //code inside };
To write function then the name after it.
Ex. function func() { //code inside};

  1. let and const are local to a block declared in cannot “see” it. var are visible throughout the whole function

  2. a specific kind value-producing function that not only has no side effects but also doesn’t rely on side effects from other code

1 Like
  1. How can you create a function in Javascript?
    With the expression and keyword function. Parameters will be set and within the body you can have statements which will be executed if you call the function.

  2. What is the difference between var and let in regards to scope?
    Let is a local binding to the block they are in. Var are visible throughout the whole function or global scope (if not within a function).

  3. What is a pure function?
    A function which produces value and has no side effects and as well does not rely on any other side effects from other functions or code segments.

1 Like
  • How can you create a function in Javascript?
    to create a function specify the keyword function then give it a name to call it, can have parameters or not between parenthesis and a body containing the code to execute between braces.

    function tocall ( parameters, oRnot ) {
    body //codeToExecute
    }

  • What is the difference between var and let in regards to scope?
    var is a global scope variable instead let(or const) is a local scope variable, meaning if we use var in a function is visible to all the function, if outside the function is visible for all the global scope of the program. let or const are local to the block they are declared in.

  • What is a pure function?
    A pure function is a function that doesn’t have any side-effects and doesn’t rely on side-effects from other code. It always produces the same value if the same inputs are passed.

1 Like