Functions - Reading Assignment

  1. with const function = function(x){return x + 2;}
    or function addtwo (x) {return x + 2;}
    or const addtwo = x => x+2
  2. Var is either global or local and let is local only I guess
  3. a value-producing function with no side effects and that does not rely on side effect from previous code.
1 Like

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

2.) In pre-2015 Javascript, only functions created new scopes, so old-style bindings, created with the var key word, are visible throughout the whole function they are in - or throughout the gobal 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 doesnt rely on side effects from other code.

1 Like

[quote=“ivan, post:1, topic:3079”]

  • How can you create a function in Javascript?
    By using the function keyword. There are different syntax options such as (i) const square = function (n) {return n * n;}; or (ii) const square = n => n * n;
    ;
  • What is the difference between var and let in regards to scope?
    The var keyword declares bindings that are global in scope. The let and const keywords declare bindings that are local to that code block.
  • What is a pure function? A pure function returns the same value for the same given arguments. It does not produce side effects like a console log, or depend on global variables that could change its output value.
1 Like

To create a function in JS you can start with or without a name binding then declare it with the function keyword followed by a set of parameters and a body.

The difference between var and let in regards to scope is that var is a global scope and can be called from anywhere. Where as let operates on a local scope, or within functions and declarations

A pure function is one without any side effects as well as the ability to purely be called without the need of other functions side effects. Additionally, it is able to be called and returned with the same value.

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

A function definition is a regular binding where the value of the binding is
a function. A function is created with an expression that starts with the keyword function.

function myFunction(x, y) {
return x * y; }

  1. What is the difference between var and let in regards to scope?
    var
    variables can be updated and re-declared within its scope and var variables are visible throughout the global scoop
    let
    variables can be updated but not re-declared also let variables are considered local to the block.

  2. What is a pure function?
    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?

  2. A function is created with an expression that starts with the keyword function in JavaScript script.

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

  4. let is only able to be visable within the function that it has been created in ( Its local environment ).
    var can be seen by the whole code ( Global environment )

  5. What is a pure function?

  6. Is a function that produces no side effects and rely on no side effects from other code.
1 Like
  1. You can create a function in Javascript through an expression that starts with the keyword function. Functions have a set of parameters (whether multiple or none at all) and the body, which contains the statements that are to be executed when the function is called. The body of a function must always be wrapped in braces, even when it consists of only a single statement. Parameters to a function behave like regular bindings, but their initial values are given by the caller of the function, not the code in the function itself. Also, functions can be created inside other blocks and functions, producing multiple degrees of locality.

  2. While VAR and LET are both used to declare variables in JavaScript, their key difference lies in their scopes:
    ‱ VAR is function or globally scoped: variable declared with var is defined throughout the program and, thus, can be accessed outside the loop; var variables can be updated and re-declared within its scope;
    ‱ LET is block scoped and can’t be globally accessed: let can only be available inside the scope it’s declared (as in ‘for’ loop) or, let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used; let variables can be updated but not re-declared.

  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. 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?
    Start whit the keyword (funtion).
  2. What is the difference between var and let in regards to scope?
    let declares a variable to the block, statement, or expression on which it is used.
  3. What is a pure function?
    A pure function is a function that given the same input will return the same output and it producess no side effects.
2 Likes

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?
let is consider local binding inside the block they declared where var is global through the whole scope.
3. What is a pure function?
A pure function doesn’t produce side effects and always return consistent value from the same input.

2 Likes
  1. function = created with an expression starting with the function keyword
  2. let= declare variables that are limited in scope to the block, var = keywords -> define a variable trough a keyword (globally or locally) to an entire function regardless of blockscope
  3. pure function = function where the return value is only determined by its input values without observable sides effect
1 Like
  1. You create a function in JS by declaring it. This can be done in a variety of ways. A function always has a declared set of parameters [which can also hold no values] in a set of brackets ( ). This is followed by the ‘body’ of the function, using the curly brackets { } - this contains the code to be executed when the function is called.

  2. The commands ‘let’ and ‘const’ create bindings connected only to the local function block that they are in. The command ‘var’ is visible beyond this in two ways - visible throughout a function block they are declared in, and If not in a function block, then visible throughout the global scope.

  3. A ‘pure’ function is one that returns no ‘side effects’ and that will also always return the same value when given the same input.

1 Like

How can you create a function in Javascript?

- ï»żYou can create a function with the keyword ‘function’, followed by a name, followed by parentheses that can contain parameters, followed by curly brackets, that contain the body, which is what will be performed when the function is called by using the name.

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

- ï»żlet is local to the block where it occurs whereas var is global in scope.

What is a pure function?

- ï»żA pure function is predictable in that it will always produce the same results given the same input. It is not dependent upon global variables whose values might change.

1 Like
  1. Function can be created by starting with keywords. Functions have parameters and based upon them, function is called and body of the function is executed.
    function name (parameters) { code }

  2. let and const declarations are local to block whereas var have global scope.

  3. Pure function gives same output provided same input is given. They are independent of outside state and produce no side effects.

1 Like
  1. A function in JavaScript is created by type keword function, name function name, then between parentheses, input parameters comma separated).
    function sum( a, b){ return a+b;}

  2. Let is a local binding while var is a global.

  3. A pure function returns the same output for a particular input; is a deterministic function;

1 Like
  1. How can you create a function in Javascript?
    You can create a function in Javascript using the command function(something){tell function what to do}
  2. What is the difference between var and let in regards to scope?
    The difference between var and let is that Var is global and Let is local.
  3. What is a pure function?
    A pure function returns a value based on the inputs of the function.
1 Like

  1. A function is wrapping a piece of program in a value, starting with "function" then the function name, and then (). Such as:
    function square (n){
    return n * n
    }
  2. Var is global in scope, and let is local.
  3. Pure functions do not rely on side effects, and will always return the same output.

1 Like

Gotta be honest, parts of this chapter went completely over my head. Is it just me or are there parts in these examples that haven’t been introduced well previously? Sure looking forward to Ivan covering this and making it seem much easier.

Questions

  1. How can you create a function in Javascript?

    We can create a function with an expression that starts with the keyword function such as

  • const insertVariable = function () {expression}

    Can also create by starting with the keyword function:

  • function insertVariable(X) {expression}

    And while Ivan told us to skip this, can also use an arrow such as:

  • const insertVariable = (parameters) => {expression}

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

    var: visible throughout the whole function that it appears in or throughout the global scope if not in a function.
    let: local to the block that it is declared in (along with const) meaning that if we create one inside of a loop, code before and after the loop cannot see it.

  2. What is a pure function?

    A pure function is a value-producing function (vs a side-effect producing function) that do not have any side effects, and don’t rely on side effects from other code either. They will work the same in any context and do not read global bindings whose value might change.

1 Like
  1. With an expression that starts with the keyword function.

  2. Old-style bindings, (pre 2015 JS) created with the var keyword, are visible throughout the whole function that they appear in—or throughout the global scope.

Bindings declared with let are in 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.

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

a. by using the function keyword;
b. by declaring a binding, then giving it a function as its value;
c. by using arrow functions

  1. Bindings created with let are local to the block. Bindings create with var are visible throughout the whole function they are in, or they are global if not a function.

  2. It is a function that produces only values with no side effects and that doesn’t rely on side effects from any other functions.

1 Like
  1. You can create a function by using the binding const, assigning a binding to be a function. You can also do a declaration of function, which doesn’t require binding. Declarations of a function can also be made AFTER it is already used in code.
  2. Var is global ins cope, while let bindings are only recognized within the block that they are declared in.
  3. It’s a function that produces a value without a side effect, but in addition does not rely on side effects from other code.
1 Like