Functions - Reading Assignment

1. How can you create a function in Javascript?

Three different ways to create a function -
Method 1: Defining ‘a’ to hold a function value
var a = function(b){
return b*b;
};
console.log(a(3));

Method 2: Declaring ‘a’ to be a function
function a(b){
return b*b;
}
console.log(a(3));

Method 3: Using arrow function
var a = b => b*b;
console.log(a(3));

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

The scope of a variable defined with let is limited to the block in which it is declared.
While the variable declared with var has global scope except when declared inside a function.

3. What is a pure function?

A function is only pure when given the same input always produces the same output.
It produces no side effects, which means that it can’t alter any external state.

Example:
Math.random() - Not a pure function
Math.max(2,5,7,8) - Pure function - output is 8 always with same set of input parameters

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

By typing a regular binding where the value of the binding is a function, like so:

const plus = function(y) {
return y+y;
};
  1. What is the difference between var and let in regards to scope?

The var variable has global scope and can be called upon repeatedly in the program, regardless of whether it’s in or out of a block. The let variable has local scope and if it’s within a block, the it can only be called upon within that block.

  1. What is a pure function?

A function that only has values and no side effects. For example:

const plus = function(y) {
return y+y;
};
1 Like
  1. How can you create a function in Javascript?
    A JavaScript function is a block of code designed to perform a particular task.
    A JavaScript function is executed when “something” invokes it (calls it).

  2. What is the difference between var and let in regards to scope?
    JavaScript variables are containers for storing data values.
    In this example, x , y , and z , are variables, declared with the var keyword, while let keyword define a variable with restricted scope.

  3. What is a pure function?
    Pure functions are functions that accept an input and returns a value without modifying any data outside its scope(Side Effects).

1 Like
  1. How can you create a function in Javascript?
    function(parameters) {
    // body with statements to be executed
    }

  2. What is the difference between var and let in regards to scope?
    “let” is a local binding within a block (loop, function). “var” is a binding within the global scope.

  3. What is a pure function?
    A pure function which only produces values and no side effects and also doesn’t rely on side-effects from other code.

1 Like
  1. How can you create a function in Javascript?
  • You can create a function using the “function” declaration, followed by the function name, arguments, then the function definition.
  • Also by creating a binding to a variable or constant and then binding that to functionname(parameters){function expressions}
  • Another way of creating a function is using arrow notation:
    var /(or const)/ functionName = (params) => {expressions that make up the function}
  1. What is the difference between var and let in regards to scope?

Variables created using var are global in scope, whereas binding delcared with const or let are local to the current scope or block that they were declared in. This means that if I delcare let x = 2 inside of a function, I cannot then later call that binding unless I intentionally force the function to return that binding as the result of the function. If I used var x = 2 inside the function, I would be able to reuse it again outside the function as needed.

  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

1 Like
  1. How can you create a function in Javascript?
  • let <func_name> = (<parameters>){<func_body>}
  • function <func_name>(<parameters>){<func_body>}
  • const <func_name> = (<parameters>){<func_body>}
  1. What is the difference between var and let in regards to scope?
<html>  
<body>
    <script>
        // calling x after definition will write 5
        var x = 5;
        document.write(x, "\n");
  
        // calling y after definition will write 10
        let y = 10;
        document.write(y, "\n");
  
        // calling var z before definition will return undefined
        document.write(z, "\n");
        var z = 2;
  
        // calling let a before definition will give error
        document.write(a);
        let a = 3;
    </script>
</body>  
</html>   
  1. What is a pure function?
  • A pure function only produces a value and does not have side effects and does not rely on the environment for its execution.
2 Likes

1. How can you create a function in Javascript?

Functions in JavaScript are 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 must be wrapped in braces. A function can have multiple parameters or no parameters at all.

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

The var keyword, as opposed to let, will be visible throughout the global scope. Whereas, a let binding will only be visible to the function it appears in (or its local scope).

3. What is a pure function?

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

1 Like

A function is created in JavaScript using three methods:

  • Define using the const variable. For example:

const f = function(a) {
console.log(a + 2);
};

  • Declare a binding with function value. For example:

function g(a, b) {
return a * b * 3.5;
}

  • Use the arrow function. For example:

let h = a => a % 3;

The difference between var and let in regards to scope is that var can be referenced globally, whereas let can be confined to within the function. What this means is when using let, the bindings declared are only used within the function parameters. Var on the other hand is stored in the program memory, not just the function used.

A pure function is a function when given the same input, will always return the same output. It also produces zero side effects nor rely on any.

1 Like

1. How can you create a function in Javascript?

Start with the keyword function, then the function name followed by the parameters within parentheses. End with curly brackets, within which you put the function’s code.

function myFunctionName(parameterA, parameterB){
//Code of function body goes here
}

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

When you declare a binding with let or const, they will have local scope, meaning they will only be available for use within the block (the curly braces { }, i.e. for, while, if, etc.) that they are declared in.

Bindings declared with var have global scope if declared outside of a function. If they are declared within a function, they are visible throughout the entire function they appear in.

3. What is a pure function?

A pure function is a well-defined function that always returns the same value when called with the same arguments and produces no side effects such as printing a line.

1 Like
  1. How can you create a function in Javascript?
    A function is created with the expression function which include parameters and a body. The body is always wrapped in braces and can contain multiple parameters or no parameters all.
  2. What is the difference between var and let in regards to scope?
    let is a local variable which belongs to the block where it appears, so the variable declared with the let keyword is limited to the block-scoped only. A variable declared with the var keyword is defined throughout the program.
    Also, one difference between var and let is variable with var can be redeclared to some other value while variable could not be redeclared if it is defined with let.
  3. What is a pure function?
    A pure function always returns the same value when called with the same argument
1 Like

How can you create a function in JavaScript?

Expressions that begin with a predefined keywork are used to create functions. They are used to sum up a series of parameters and to be implemented when called upon.

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

Var scope continues to exist outside of a block, while let scope only exists within blocks.

What is a pure function?

A pure function is one that has no side effects and does not depend on the side effects of other functions.

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?
    When you declare a binding with let or const , they will have local scope, meaning they will only be available for use within the block that they are declared in.

Bindings declared with var have global scope if declared outside of a function. If they are declared within a function, they are visible throughout the entire function they appear in.

  1. What is a pure function?
    A pure function is a function when given the same input, will always return the same output. It also produces zero side effects nor rely on any.
1 Like
  1. Binding a name for a function with const, then describing a function and parameters used in it.
  2. Bindings declared with let are local to the block they are described in. Bindings declared 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 doesn’t have any side effects and it doesn’t read global bindings. When calling a pure function with the same arguments, it always returns the same value.
1 Like
  1. function name(parameters){program}
  2. let declares a variable limited to a block, whereas var defines a variable reguardless of the block
  3. A pure function is a function that is completely independent of the outside, it don’t interfere with it.
1 Like

1. How can you create a function in Javascript?
You can create a function by using the keyword “function” along with parameters and body which contains a statement to be executed when the function is called. Body should always be wrapped in braces.

2. What is the difference between var and let in regards to scope?
Var - is visible throughout the whole function. Throughout the global scope.
Let- is a local binding. It can only see within its function parameters.

3. What s a pure function?
Pure function is a function that always returns the same result if the same arguments are passed. A pure function does not produce any side effects.

1 Like
  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 (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.
    A function can have multiple parameters or no parameters at all.
  2. What is the difference between var and let in regards to scope?
    let - local scope to the block and can not be seen outside of it;
    var - global scope, end up in the nearest function scope or the global scope.
  3. What is a pure function?
    no side effects
1 Like
  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.

  2. What is the difference between var and let in regards to scope?
    Var - is visible throughout the whole function. Throughout the global scope.
    Let- is a local binding. It can only see within its function parameters.
    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.

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

1 Like
  1. How can you create a function in Javascript?
    Starts with function keyword, followed by a name and then parameters within parentheses. function name(parameter(s))

  2. What is the difference between var and let in regards to scope?
    The var keyword’s function parameters are seen throughout the global scope and the let keyword is local (not seen by code before or after the loop).

  3. What is a pure function?
    A pure function has same inputs always returning same outputs (consistent results) and has no side effects

1 Like

Functions - Reading Assignment

You can type “function ()” // declaration of function
or
“const x = function()” // function as binding

  1. Var is a global binding - it means the variable is set to the whole program.
    Let is a local binding - it defines the variable for the inner-most loop, for the local subfunction.

  2. Pure function is a function that does not produce side effects. Such function is for ex. function that calculate a mathematical problem.

1 Like
  1. How can you create a function in Javascript?
    A function is created with the keyword function. Functions have a set of parameters and a body, which contains the statement.

  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, if it is in a loop the program before it cannot see it. The keyword var is global or local to an entire function.

  3. What is a pure function?
    A pure function is a specific kind of function that has no side-effect, but it also produces a value.

1 Like