Functions - Reading Assignment

  1. How can you create a function in Javascript?
    A JavaScript function is defined with the function keyword, followed by a name and by parentheses
  2. What is the difference between var and let in regards to scope?
    Both are keywords in JavaScript used to declare variables. scope determines where variables are visible in our script.
    Every JavaScript function has a scope, hence variables declared in a function may only be visible and used within that function. Global variables, on the other hand, may be accessed from any part of the script.
    The main difference between these two is that variables declared with var are function scoped, whereas those declared with let are block scoped.
    both var abd let are used to declare variables
  3. What is a pure function?
    in computer programming, a pure function is a function that has the following properties: 1. The function return values are identical for identical arguments. 2. The function application has no side effects.
2 Likes
  1. How can you create a function in Javascript? By declaring a function and then invoking it
function functionName (parameters) {
 return expression;
}

or by storing it in a variable

var variableName = function (parameters) {
 return expression;
}
  1. What is the difference between var and let in regards to scope? Var is visible throughout the whole function that it is in, or global if outside of a function. Let is local to the block it is created in
  2. What is a pure function? A specific kind of value-producing function that doesn’t have any side effects or rely on a global binding that may change. It always produces the same value if the same inputs are passed
1 Like
  1. How can you create a function in Javascript?

You can create a function by declaring it keyword Function, or by creating a Variable function. eg.

function greet () {
console.log (“Good day!” );
}
greet();

or

Const speak = function () {
console.log(“Hello there”);
} speak();

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

Var (Variable) are use or seen in a global scope. while Let are use and seen in a Local scope.

  1. What is a pure function?

A pure function is one that returns a value, has no side effects, and also doesn’t rely on side effects from other code.

1 Like
  1. you could do it this way:
const someFunction = function(a,b) {
   return(a*b);
}

or that way:

const someFunction = (a, b) => a*b;

2.“var” is not local to the block it is declared in while “let” is.
3. a pure function (as in solidity) only uses it’s arguments and local scope for its calculations.

1 Like
  1. How can you create a function in Javascript? Function is defined with the function keyword, parameters () and a body that contains the statement***

  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.
    It can be said that a variable declared with var is defined throughout the program as compared to let.

  3. What is a pure function? Given the same input, will always return the same output. Produces no side effects.***

1 Like
  1. A function is created with an expression that starts with the keyword function. This word has a set of parameters and a body which contain statements to be executed once the function is called.
  2. When it comes to scope, let is local to the block that is declared in. It is declared inside a function and being referenced only in that function, as a local biding. Var is visible throughout the whole program once the biding is defined outside of the function or block, the so-called global biding.
  3. A pure function is a specific kind of value-producing function that not only has no side effects but also does not rely on side effects from other code – for example, it does not read global bindings whose value may change. A pure function 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 created with an expression that starts with the keyword function. Functions have a set of parmeters 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?
Bindings declared with let and const are in fact local to the block that they are declared in, so if you create one of these inside the loop, the code before and after the loop cannot »see« it.
Only functions 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?
It is a function that 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. The function does not produce any side effect (network request, input/output devices or data mutation).

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. 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 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. Var 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 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. Functions are created by starting with the function keyword and creating a function body that may or may not utilize parameters that you define after the function call.
  2. var is visible throughout the global scope, whereas let remains in the local scope in which it has been defined.
  3. Pure functions accept values and return values, but don’t rely on side effects.
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 statements that
are executed when the function is called. The body of a function
created this way is 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 and const use block scoping and won’t appear locally.
Var uses the global scope if they are not declared in a function.

3. What is a pure function?

A pure function has no side-effects and always produces the same value when called with the same arguments.

1 Like
  1. You can create a function by either creating a variable and assigning a function to it, or creating a function and naming it like this: function myFunction() {}; Also, you can create so called arrow functions which is just an easier form of the second method.

  2. If you create a variable using let, it’ll only exist inside the scope. However, a variable created with var, will exist globally in the program.

  3. A pure function is easy to understand, does one specific thing, is not affected by changes in other parts of the program, returns a value and doesn’t have side effects.

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

  2. Let can only be used in a block scope whereas var can be used in a function scope which means it can be used globally. (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. The function return values are identical for identical arguments. The function application has no side effects.(like a mathematical function)

1 Like
  1. We can pass a function as a value to a variable/const.
    const f = function(a) {
    console.log(a + 2);
    };

We can just declare a function:
function g(a, b) {
return a * b * 3.5;
} //In this case, the semicolon at the end isn’t required

We can make arrow-style declarations:
let h = a => a % 3;

  1. Bindings declared with “let” are local to their block. Binding declared with “var” are visible throughout a function, or if not in a function, are globally visible.

  2. A pure function has no side effects, produces a value, and doesn’t depend of any external side effects for its operation.

1 Like
  1. How can you create a function in Javascript?
    When the function
    keyword is used at the start of a statement
  2. 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.
    var keyword, is visible throughout the whole function that they appear in—or throughout the global scope
  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.
1 Like
  1. How can you create a function in Javascript?
    let functionName = function(parameter){body};
    function functionName(parameter){body}

  2. What is the difference between var and let in regards to scope?
    var can be utilized globally while let and const are within thier block of declared within function, aka block scoping/local scope.

  3. What is a pure function?
    A pure function accepts input that are not side-effect of other codes and produces output with no changes to external variables or exhibit side effect by its execution.

1 Like
  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 statements that are to be executed when the function is called. A function can have multiple parameters or no parameter at all. Example:
const power = (base, exponent){
  let result = 1;
  for (i=1; i<=exponent; i++){
    result *=base;
  }
return result;
}

It also possible to create a function by function declaration:

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

or by arrow functions.

  1. Bindings declared with let and const are local to the block that they are declared in. Bindings created with the var keyword are visible throughout the whole function they appear in or throughout the global scope if not in a function.

  2. A pure function is a value-producing function that has no side effects and doesn’t rely on side effects from other code. A pure function called with the same argument will always produce the same value.

1 Like

1. How can you create a function in Javascript?

There are three formats for creating functions in JS.

First:
const exampleFunction = function(parameters) {
body;
};

Second:
function exampleFunction(parameters) {
body;
}

Third:
let exampleFunction = parameters => body;

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

Let is local in scope and var is global in scope. If let is used within a function/block of code, the binding it sets can’t be seen outside the of that block. Conversely if var is used, it can be seen anywhere inside the entire program.

3. What is a pure function?

A pure function is a function that is deterministic, always returning the same value if it is given the same argument. It also does not produce any side effects.

2 Likes
  1. How can you create a function in Javascript?
    3 different ways

    FIRST) by defining it as a “const” variable and including the keyword “function()”
    Example:
    const square = function(x) {
    return x * x;
    };

SECOND) Declaration notation:
Example:
function square(x) {
return x * x;
}

THIRD) Arrow notation:
Example:
const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};

  1. What is the difference between var and let in regards to scope?
    Var is more global. Let is for more localized use.

  2. What is a pure function?
    It produces a value, but doesn’t produce side-effects and doesn’t rely on other side effects.

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

JS recognises the keyword function
or the arrow => notation
(followed by the right parameters and body)

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

Var is recognised in the global scope, let is used in local scope

  1. What is a pure function?

a function without side effects

1 Like

How can you create a function in Javascript?

By creating a binding value to the constant;can be created with declaration notation, an Arrow notation, or just by using “const” variable with the function().

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

Var can be used “globally” or throughout the code, while let is used within the local block it is in.

What is a pure function?

One that only produces a value and has no side effects

1 Like