Functions - Reading Assignment

  1. How can you create a function in Javascript?
    With a Function keyword, followed by name and parantheses Function name () {code}
  2. What is the difference between var and let in regards to scope?
    let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. var keyword defines a variable globally, or locally to an entire function regardless of block scope.
  3. What is a pure function?
    A function that does not have any side effects and doesn’t rely on side effects of other code. It always produces the same value when called with the same arguments and doesn’t rely on global bindings because they might change.
1 Like
  1. function can be created by using the ‘function’ expression which contains certain sets of instruction and then assigning this to a binder for use whenever needed.
  2. with let you can create binders that are restricted to the expression in which it is used or to the block and statement.
    var creates binders that are available globally or to the function it is defined.
  3. Functions which gives the same output for the same input is called pure function. It doesn’t have any side affects.
1 Like
  1. A function is an expression created with the keyword Function at the start. They have a set of parameters and a body which contains the statements that are to be executed when the Function is called.
  2. Bindings declared with ‘let’ are limited in ‘scope’ to the local block, when binding with ‘var’ they have a global scope as long as it is not within the function.
  3. A pure function is a specific value producing function that has no side effects nor relies on any side effect.
1 Like

How can you create a function in Javascript?

A function is created with an expression that starts with the keyword function

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 are visible throughout the whole function that they appear in or throughout the global scope, if they are not in a function.

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?

With the keyword ‘function’ followed by a list of zero or more arguments in parenthesis. Functions that generate a value can do so with the ‘return’ keyword. There are 3 ways to create function bindings:

  • Assign a function to variable
  • Declare a function on it’s own using the function keyword
  • Using arrow syntax
2. What is the difference between var and let in regards to scope?

Bindings that use let recognize nested scopes within code and are only visible within that local scope. Bindings made with var have no concept of nested scopes (apart from functions). I.e. if you declare x within an if block it will be accessible outside the if, but with let or const it would not.

3. What is a pure function?

A pure function has no side effects and is completely self contained- it doesn’t rely on any outside state- given the same input it will always return the same output.

2 Likes
  1. A function is created with an expression that starts with the keyword function
    Example: const f1 = function() { //code };
  2. Bindings declared with var affect the nearest function scope or the global scope. Let declares variable limited to a relevant block, statement or expression.
  3. A pure function makes zero side effects but also relies on zero side effects at the same time. It is therefore determined only by input values.
1 Like
  1. const bitcoinHalving = function (x) {
    return x * x;
    };
    A function definition is a regular binding where the value of the binding is
    a function.
  2. The let keywords declares a variable with its scope limited to the block on which it is used, while var defines a variable with a larger scope. Local within a function or global.
  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?
    There are 3 ways:

-Defining a function looks like this

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

-Declaration notation looks like this

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

-Arrow functions look like this

const square = (x) => { return x * x; };
  1. What is the difference between var and let in regards to scope?
    Bindings declared with ‘let’ are local to the block they are declared in- ie. if ‘let’ is declared inside a loop, the code before and after the loop cannot read the ‘let’ binding.
    Bindings created with ‘var’ are visible throughout the entire function they appear in- or globally, if declared outside of a function.

  2. What is a pure function?
    A pure function is a value-producing function that has no side effects and doesn’t rely on side effects from other code. When called with the same arguments, a pure function always returns the same output.

1 Like
  1. Here are some ways of creating functions:
    const square = function(x) {
    return x * x;
    };
    //const or let could be used

function square(x) {
return x * x;
}
//another way of defining a function

const square = (x) => { return x * x; }; // This is an ‘arrow’ function

const square = x => x * x; // This only works when there is only one argument

2 . Var is scoped to the function body, Let is scoped to the enclosing block {}

  1. A pure function depends only on its own input arguments and returns the same result if the same arguments are passed.
1 Like

chapter 3:

How can you create a function in Javascript?

A function is created with an expression that starts with the keyword function. Also needs a set of parameters, and a body. The body has the statements that are to be executed when the statement is called. -This expression calls the computer to action.A function is a call to action, ie…consol.log(x); return results;

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

Bindings that declare Let apply to just the function at hand. It’s safer than var.

Bindings that declare var apply the function globally, affecting future unrelated strings. Because of this, it should be avoided

example:

for(let i=0; i<5; i++ ){

consol.log(i)};

console.log → produces results

a second time even without {}, you get an error

start();

If you swap “let” for “var”, the second time there would be no error message.

  1. What is a pure function?

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. It doesn’t read global bindings, and will always give the same results. A math function is an example. The book states that there is no need for a holy war against pure functions. Console.log would not work with a pure function.

what cool concepts!! I’m enjoying this class!!! Thanks team for making it interesting, and practical.

1 Like

1. How can you create a function in Javascript?

By using an expression that starts with the keyword function.
Functions also have a set of parameters and a statement that are to be executed when the function is called.

for example;

const square = function (x){
return x * x;
}
console.log (square(10));

// → 100

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

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? A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ( ). function name(parameter1, parameter2, parameter3) {code to be executed}

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. “Let” can only be accessed from within the function, whereas “var” can be accessed from both inside and outside the function.

3. What is a pure function? Pure functions are functions that accept an input and return a value without modifying any data outside its scope. Not only does it not have any side effects, it doesn’t rely on side effects from other code.

1 Like
  1. by declaring the function keywords, followed by expression, a semicolon and a bracket of statement
  2. the scope of Let are smaller, but it is more pure, while Var has wider scope, and it can cause side effect when it is used inside a block
  3. the pure function is a straight forward command which will always result as its expected
1 Like
  1. How can you create a function in Javascript? The function keyword, when used as an expression, can create a function value. Eg: function g(a, b) { return a * b * 3.5;}

  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” on the other hand defines a variable globally, or locally to an entire function regardless of block.

  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, then name followed by parameters ().

function name(parameter1, parameter2, parameter3) {code to be executed}

  1. let is only visible within the function that it is created in and var can be called from outside the function.

  2. A pure function is a function that does not create any side effects or rely on side effects

1 Like
  1. Using ‘function’ keyword in either defining a function as a value assignment or declaring a function placing the ‘function’ keyword in first place
  2. The “var” variable visibility is restricted to the function or main_global scope, while the “let” variables are visible only to the inside code block they are declared at.
  3. A “pure” function is like a math function that generates a value (and does not depend on global environment state or from other non pure functions or side effects).
1 Like
  1. Functions are created by using the keyword function and a name to bind the block of code which will produce the value.
  2. Var has a function-wide scope and let has a block-wide scope.
  3. Pure functions have no side effects and always return the same value regardless of environment. They can be substituted for the value which they produce without any change to the code’s effect.
1 Like
  1. In Javascript, a function is created by being declared. The keyword “function” is used at the beginning of an expression to have a binding to a body being the set of instructions to be executed
  2. The scope is limited for the keyword let but global for var
  3. A pure function is one that only outputs values, is deterministic and does not create side effects
1 Like
  1. How can you create a function in Javascript?
//Function value definition
let/const/var functionName = function(arg1, arg2, ...) {
  //Code to execute
  };

//Declaration notation
function functionName(arg1, arg2, ...) {
  //Code to execute
  };

//Arrow notation
let/const/var functionName = (arg1, arg2, ...) => {
  //Code to execute
  }

//Arrow notation can even be shorter if there is only one or no argument(s) and only one expression in the function body
let/const/var functionName = () => some + expression; //or
let/const/var functionName = arg1 => some + expression;
  1. What is the difference between var and let in regards to scope?
    Bindings created with the var keyword are visible throughout the whole function that they appear in, regardless if they are created in a block (like a loop-block). If var-bindings are not created in a function they have global scope (in the whole program). Bindings with let and const are local to the block that they are declared in, and thus they are not visible outside that block elsewhere in the same function.

  2. What is a pure function?
    A function that return a value and doesn’t create ant side effects, nor does it rely on side effects from other code. When a pure function is called with the same arguments, it will always return the same value.

1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.