Functions - Reading Assignment

  1. How can you create a function in Javascript?
    A function can be created by with an expression that starts with the keyword function.

  2. What is the difference between var and let in regards to scope?
    Var defines a global variable that is visible throughout the function they appear in or throughout the global scope. Where as let defines a variable that can only be used within its block or function (even repeated functions can not see a let variable defined outside of it) and are known as local bindings.

  3. What is a pure function?
    pure functions only produce values (no side effects), and do not rely on any side effects from other code. It doesn’t read global bindings whose value might change.

1 Like
  1. function functionName(parameters){code }
  2. let has a scope of the block that it is declared in. var has a global scope.
  3. A pure function is a function that only accesses information within its own scope.
1 Like
  1. You create function with an expression that starts with the keyword function.

  2. Let and const are local to the block they are declared in, so it can only be used within that block. var is global, so it can be used throughout the whole function it is declared in, or if not in a function, it can be used globally.

  3. 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 Like
  1. How can you create a function in Javascript?
    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};
  2. What is the difference between var and let in regards to scope?
    let and const use block scoping, they won’t appear locally. Var uses the global scope if they are not declared in a function.
  3. What is a pure function?
    It doesn’t produce or rely on side effects, avoids reading global bindings.
1 Like
  1. Method 1:
function nameOfFunction(param 1, param 2, param 3){
statement1;
statement2;
........
}

Method 2:

const variable_name = function(param 1, param 2, param 3){
statement1;
statement2;
.........
}

Method 3:

const variable_name = (param 1, param 2, param 3) => {
statement1;
statement2;
.........
}
  1. The difference between is that var type variables have bindings that are global in scope, whereas let type variables have bindings local to the block. Also, any type of variable declared inside a function will always be local to it, meaning it won’t be accessible outside the function.

  2. A pure function is a specific type of value returning function that cannot read global bindings whose value might change. Because of this property, it does not rely on side effects from other functions nor create them.

1 Like

1 - The function is created using an expression that begins with keyword function. Functions contain a set of parameters (), these define the function. From there we have the body, which contains the statements that will be executed when the function is called upon. A function can have as many parameters as is desired. The values of the parameters can be whatever the caller assigns. Not all functions have to produce a value, some will only produce side effects.
2 - Bindings declared using let are local to the block that they are declared within. Bindings declared using var are global and are not limited to the that they are declared within.
3 - A pure function is a function that will produce the same output as long as the input is the same, they will also not produce side effects. An example is:
const double = x => x * 2;
console.log(double(5));

This is a pure function because its output is determined by the input, and it also will not produce any side effects.

1 Like

1. How can you create a function in Javascript?
We can create a function in JavaScript by creating an expression that starts with function. Structure:
const funcName = function(parameters) {body of function where statements are contained};

2. What is the difference between var and let in regards to scope?
Let and const are local bindings in the block where they reside. Var has a global scope.

3. What is a pure function?
A pure function is a value producing function that has no side effects nor does it rely on side effects from other code.

1 Like
  1. Function declaration.

Functions are created using the key word “function” and has the following syntax.

function fName ( arguments) { body - code to perform operation, optional return value};
arguments and return value are optional

function add ( a,b) { let c= a+b); return c; }
  1. Scope -Variables (bindings) declared by “let” have their scope in the block {…} for which they are declared, as well as in any contained sub-blocks. In this way," let" works very much like “var”. The main difference is that the scope of a “var” variable is the entire enclosing function: See below

function varTest() {
var x = 1; // x declared in outer block
{
var x = 2; // same variable re-declared replaces outer x
console.log(x); // print 2
}
console.log(x); // print 2 inner block declaration of x
}

function letTest() {
let x = 1; // x declared in outer block
{
let x = 2; // new variable x declared in inner block
console.log(x); // print 2
}
console.log(x); // print 1 outer block declaration of x
}

  1. Pure function has the following properties:

a) It depends only on it’s own arguments.

b) It won’t try to change variables out of it’s scope ( out side the function).

c) It does not produce any side effects.- like console output or user input for example.

1 Like

1. How can you create a function in javascript?
a. functions contain a block of code specifically for task related performance. It consists of an
expression with a keyword:”FUNCTIONS”, with parameters()and a body{}.

2. What is the difference between var and let in regards to scope?
a. The difference is block related. LET is used for local scope and does not work outside the block,
VAR is used for global scope and does work outside the block.

3. What is a pure function?
a. A pure function produces no side effects whatsoever, the same input produces the same
output.

1 Like
  1. By creating a binding and using the keyword function(x){y}; x being the parameter/s of the function and y being the expressions executed using x parameter/s

  2. var is visible globally given it is not in a function and let is visible locally

  3. It is a value-producing function that has no side effects and does not rely on side effects of other code

1 Like
  1. With an expression that starts with the keyword ‘function’, contains a set of parameters and a body with statements to be executed eg: const square = function(x) {
    return x * x;
  2. let bindings have a local scope to the block they are declared in whereas var bindings will be recognised within the whole function or throughout the global scope if they are outside a function.
    3.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? A keyword for the function is defined by the creator in an expression. The function has parameters. In the body of the script the parameters are executed as per the statement instructions when the function is called. The body is wrapped in braces.

  2. What is the difference between var and let in regards to scope? let is local, and can only been seen in the block there are declared in. Var is global if not in a function. If in a function, it is seen throughout the whole of the function.

  3. What is a pure function? Has no side effects, and does not rely on side effects from another function. It does not read global values. When called with same arguments, it produces the same value every time.

1 Like

How can you create a function in Javascript?

A function is created with an expression that starts with the keyword function , they have a set of parameters and a body which contains the statements that are to be executed.

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

They behave differently:
var bindings end up in the nearest function scope or the global scope
let bindings end up in the local scope and can not be seen outside of it

What is a pure function?

A specific kind of value producing function with no side effects and it does not rely on side effects from other code.

When called with the same arguments it always produces the same value.

1 Like
  1. How can you create a function in Javascript?
    A function is defined using the keyword function, followed by name, followed by parentheses containing the arguments of the function, followed by the body of the function with curly brackets

  2. What is the difference between var and let in regards to scope?
    let and const are local to the block they are declared in, so it can only be used within that block. var is global, so it can be used throughout the whole function it is declared in, or if not in a function, it can be used globally.

  3. What is a pure function?
    A pure function is a value producing function that can be substituted with its return value without changing the meaning of the code. A pure function does not product a side effect and does not rely on global binding that may change. When called with the same arguments, it always produces the same value.

1 Like
How can you create a function in Javascript?
   <type> <function_name> = function(<any inputs>) {
        <execution/actions>
        return <result>;
   }

   Then you call the function:
      answer = <function_name>(<any inputs>;

   Example add:
      const myadd = function(num1, num2) {
         result = num1 + num2;
         return result;
      }
      answer = myadd(5, 9);
      console.log(answer);

What is the difference between var and let in regards to scope?
    let and const are local to teh block they are created in (for example inside a loop)
    var is visible throught the whole function they appear in or outside the function to global.

What is a pure function?
    It is self-contained, it has no side effects and does not rely on side affects to other codes.
     * It does not read nor write to any global variables.
     * the return value is only affected by the inputs.
1 Like
  1. How can you create a function in Javascript?
    simply call it ( name it) and specify it’s parameters.
  2. What is the difference between var and let in regards to scope?
    let defines a binding, while var incudes type complication
  3. What is a pure function?
    produces no side effects, and will aloways give same output when it’s given the same parameters.
1 Like
  1. Create a name, and build the construct to define it. const functionname = Function (parameter){}
  2. Let can only exist within the local scope while Var can exist either in local or global scopes.
  3. It has a clean heart… jkjk it is a function that does not create side effects or take in any side effects from other functions.
1 Like
  1. How can you create a function in Javascript?

By using the key word function followed by the name of the function followed by parentheses followed by start function bracket with code inside followed by ending function bracket.
There be more than one parameter.

  1. What is the difference between var and let in regards to scope?
    var has global scope if not declared inside block whereas let is only visible inside the block where it is declared.

  2. What is a pure function?
    A pure function is a function producing the same results if the same inputs are used and which neither creates nor uses side effects.

1 Like
  1. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs.

  2. 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. Pure functions always returns the same result if the same arguments are passed in. It does not depend on any state, or data, change during a program’s execution. It must only depend on its input arguments. and dont have side effects.

1 Like
  1. How can you create a function in Javascript?
    By typing
    function “functions name” (argument1, argument 2){body function here}
  2. What is the difference between var and let in regards to scope?
    var is a global binding, that whole program scope. let is always local program scope to the block in which they are declared
  3. What is a pure function?
    function that is function that returns a value, and produces no side effects
1 Like