Functions - Reading Assignment

  • How can you create a function in Javascript
    I create a function by defining a binding with a value as a function. It needs to have a keyword, a set of parameters, a body and should always be wrapped in braces. It can have multiple or no parameters, and it can be passed by the caller.

  • What is the difference between var and let in regards to scope?
    Bindings defined outside the function are global and affect the whole program. Within the function are local and work only within the block.
    Bindings declared with let and const are local.

  • What is a pure function?
    Is a value-producing function that doesnt have side effects and always produces the same value.

2 Likes
  1. How can you create a function in Javascript?
    A function in Javascript is defined by the arrangement:
    const function_name = function(parameters) { body }
    where:
    const and function - are Javascript terms
    function_name - is the function identifier chosen by the programmer
    parameters - are variables being passed into the body
    body - is a section of code representing the function
    or
    function function_name(parameters) { body }

  2. What is the difference between var and let in regards to scope?
    var is global in scope and let is local to the block of code where it is defined

  3. What is a pure function?
    A pure function has no global bindings, and also causes no side effects. It returns a value.

2 Likes

1).How can you create a function in Javascript?
Javascipt function is defined with the function keyword, followed by a name, followed by parentheeses.

2)What is the difference between var and let in regards to scope?
Main difference is that the scope of a variable defined with let is limited to the block in which itis declared while variable declared with Var has the global scope.

  1. What is a pure function?
    A pure function is a function which given the same input, will always return the same output. produce no side effects.
1 Like

a. by creating a binding that, instead of a value, contains an expression that uses the keyword “function”, followed by zero, one or more parameters.
the syntax is:
const nameoftheconst = function(parametername)
{
//function code;
};

b. by creating a function and calling it later, e.g.
function nameoffunction(parameters)
{
//function code;
};
console.log(functionname(parametervalue));

  1. A scope in JavaScript can be imagined as the “environment” in which a binding is visible. The scope can be global (visibility in the whole program) or local (visibility limited to a function or block within the program).
    While let (and const) bindings have a local scope, var bindings have a global scope.

  2. A pure function does not produce side effects and does not depend on side effects from outside the function. They can work in every part of the program because they do not rely on external variables or functions and do not influence external variables or functions.

1 Like
  1. Start with the keyword ’ function’. Assign parameters and a body to be executed when the function is called.

2.var, when assigned outside a function is a global binding. let( usually assigned with a function), is a local binding.

3.A pure function does not produce side effects or rely on side effects. It produces the same value whenever called.

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

  2. What is the difference between var and let in regards to scope?
    var has global scope whereas let is local.

  3. What is a pure function?
    Pure function has no side effects. It also does not rely on side effects from other code.

2 Likes
  1. We can create function in 3 way:
// Define example to hold a function value
const example = function(a) {
   console.log(a*10);
};

// Declare example to be a function
function example(a, b) {
return (a * b) +10 ;
};

// Instead of the function keyword, we use an arrow =>
let example = a => a % 3;
  1. Biding declared with let scope are local and not visible from the outside of the block. Bindings declared with var behave differently, they end up in the nearest function scope or the global scope.

  2. A pure function not only has no side effects, but also doesn’t count on any other side effects through other code.

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 the statement, and is executed when the function is called.

    A function can have multiple parameters, or none at all.

  2. Bindings that are created with let and const are local to the block that they are declared in. So, if you create one inside the loop, the code before and after the loop cannot “see”it.

    Old style bindings created with var are visible throughout the whole function that they appear in, or throughout the global scope, if they are not in 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.

1 Like

How can you create a function in Javascript?
by creating an expression that starts with “function”.
function contains a set of parameters and statements to be executed
What is the difference between var and let in regards to scope?
var is global and let is local
What is a pure function?
a special type of code that has no side effects and doesn’t rely on side effects from other code

1 Like
  1. How can you create a function in Javascript?
    There are few ways. The simplest explanation would be:
    give name, pass args or not, Curly brackets for the functions body.
    It can be even simpler if using the arrow functions.

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

  3. What is a pure function?
    The function which is returned without any side effects.

1 Like

Reading Assignments: Functions.

  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function.
    It will also include with set of parameters, and 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?
    let creates “local” variable of binding inside a block, where other blocks outside the block, would not be able to see that variable.
    The var keyword creates “global” variable binding where the other blocks outside of it would be able to see that variable.

  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. Use the keyword function followed by the name of the function .
      After the function name, open and close parentheses.
      After parenthesis, open and close curly braces. Within curly braces,
      write your lines of code.
  1. The main difference is the scope difference , while let can be only available inside the scope it’s declared, like in for loop, var can be accessed outside the loop for example. … This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.
  2. A function is a process which takes some input, called arguments , and produces some output called a return value . Functions may serve the following purposes.
1 Like

They’ve completely lost me with some of the jargon in this chapter, specifically where they talk about closure. I don’t understand the concept at all. Maybe if I understood why this function works as it does, I might start to understand. Why does the 5 in twice(5) seem to be assigned to “number” to arrive at the anwwer of 10?

function multiplier(factor) {
return number => number * factor;
}
let twice = multiplier(2);
console.log(twice(5));

// → 10

Q1: How can you create a function in Javascript?
A1: A function is created with an expression that starts with the keyword “function”. It usually contains a set of parameters and will have a body which contains statements that are executed when the function is called - sample code as follows;

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

Q2: What is the difference between var and let in regards to scope?
A2: “let” (and “const”) variables are (scoped) local to the block that they are declared in whereas “var” defined variables are globally scoped

Q3: What is a pure function?
A3: A pure function can be referred to as a “value-producing” function. It has no side effects and also doesn’t rely on side effects from any other piece/block of code

1 Like
  1. How can you create a function in Javascript?
  2. What is the difference between var and let in regards to scope?
  3. What is a pure function?

Answers
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 can be declared using var or let. Bindings declared with var have a global scope while those declared using let have a local scope.
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. When called with same arguments, it always produces the same result and doesn’t do anything else.

1 Like
  1. functions are used so we dont have to repeat ourelves and just change the parameter to get a different output.
    function name(p1,p2,…) {code to execute}

  2. var can be globally, let is limited to a block (for, if, while,…)

  3. a pure function behaves in a predictable way depending on its input parameters.
    not influenced by global variables.

1 Like
  1. const f = function(a) {
    ;
    };
    or
    function g(a, b,…) {
    ;
    }
  2. var globally , let locally in expression
  3. function with no side effect
1 Like
  1. You can create a function in Java through an expression that starts with the keyword function,
  2. Let and const only exist in the local scope while Var can exist in local or global scopes.
    3 . A pure fuction have no side effects, the same input will equal the same output.
2 Likes
  1. You create a function by binding the name of the function with the function which consists of 0 or more arguments and the code to be executed.
  2. var in a function allows the variable to be used by the code inside and outside of the function where let restricts the scope to inside the function.
  3. A pure function produces only values, not side effects and doesn’t rely on side effects from other functions.
2 Likes

Hi @tbaker,

Closures are a very advanced topic in javascript. It is usually the defacto question for any Javascript interview you go to. So, it is completely understandable to not get it right even after many tries.

I will not try to explain closures my self because there are experts on youtube who can articulate it better with flow diagrams and video explanations.

However, I will make you understand how it works with your specific example –

The multiplier is a function that returns another function.

let twice = multiplier(2);.
Here, we are executing the multiplier function by passing 2 as the argument. This will return another function number => number * factor. This new child function takes "number" as an argument and multiplies with "factor". The value of "factor" is retained in this new child function because it was passed initially to the parent function. The parent function enclosed or sealed or kept in context the value of "factor"

When the child function is now called, (by calling “twice” ), it takes in the argument “5”, assigns this to the "number" argument and multiplies it with "factor". The "factor" value was retained because of the parent function execution. In a way, the parent “enclosed” the initial argument (with value 2). That’s why its called a “closure”. The first function “encloses” the value into the second function.

Hope I made you understand this topic, it will take time getting used to but just know that , if you understand this, you have a better understanding of javascript than 90% developers.

Hope this helps.

2 Likes