Functions - Reading Assignment

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

2: let is local, while var is global

3: A pure function does not have any side effects, nor relies on those from other code, to produce value.

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?
    The difference between them is that var is function scoped and let is block scoped.
    The var variable can be used gobally and let is limited to the block.

  3. What is a pure function?
    A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument.

1 Like
  1. How can you create a function in Javascript?
    let y = function(x){formula}
  2. What is the difference between var and let in regards to scope?
    let = local on block; var = global
  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

How can you create a function in JavaScript
Function name(parameter1, parameter2, parameter3) {code to be executed}
A function is a set of parameters, containing statements to be executed.

What is the difference between var and let in regards to scope?
var: visible throughout the whole function/global scope if not in a function
let: Local to the block declared in

What is a pure function?
Does not create side effects or rely on side effects

1 Like
  1. You start a function using the function keyword
    function name(p1, p2){}
  2. Let creates a variable limited to the block it is declared in
    Var defines a variable locally to an entire function or within the whole program
  3. A pure function is a specific kind of value-producing function that not only has no side effects from other code but also doesn’t rely on side effects from other code
1 Like
  1. Functions are created using the keyword function, sometimes followed by a set of parameters and a body which contains the statements that are to be executed.
  2. The let keyword works like var but creates a variable that is local (not global) to the enclosing block. Var is global and can exist throughout the entire program unless it was created within a function, in which case it will only be local to that 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 - eg/ it doesn’t read global variables that are occasionally changed by other code. When a pure function is called with the same arguments, it always produces the same value and doesn’t do anything else.
1 Like
  1. You start an expression with the keyword function, with a parameter and a body.
  2. Var is function scoped while let is block scoped.
  3. A pure function is a block of code that always returns the same result if the same arguments are used.
1 Like
    1. A function is introduced with the keyword function. The function can have parameters. and a body. The function body must be wrapped in braces.
  1. “let” within a scope is a local binding within its own local environment; whereas “var” are functions in new scopes, so old-style bindings, created with the “var” keyword, are visible throughout the whole function that they appear in; or throughout the global scope; provided that they are not in a function.
  2. A pure function is a specific kind of value-producing function that has no side effects or rely on side effects from other code. It also doesn’t read global bindings which can change.
1 Like
  1. You define a function much like defining a variable:
    function test(){ function code};

  2. The difference is that var creates a variable that can be referenced within the global scope, let creates a variable that can only be referenced in the immediate enclosing block.

  3. A pure function always gives the same output, providing the same input is provided. It also does not produce any side effects.

1 Like
  1. How can you create a function in Javascript?
    By creating a expression using the word function.

  2. What is the difference between var and let in regards to scope?
    Let is local to the block it is declared in. If located inside of a loop it cannot be seen by the code before or after it. Var keyword defines a variable globally regardles of the block scope

  3. What is a pure function?
    A pure function always returns the same result given the same parameters.

1 Like
  1. How can you create a function in Javascript?
  • For the example below:
    const square = function(x) {
    return x * x;
    };
    console.log(square(12));
    // → 144
    A function is created with an expression that starts with the keyword function. Functions have a set of parameters (in this case, only x) and a body, which contains the statements that are to be executed when the function is called.
  1. What is the difference between var and let in regards to scope?
  • var allows the variable to be used globally whereas let only allows the function assigned to use it.
  1. 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?
    You can create a function with an expression that starts with the keyword function.
  2. What is the difference between var and let in regards to scope?
    If you use the var in a loop it is visible in the code before and after. If you do a let the code before and after the loop cannot see it. (invisible)
  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 can be created like this:

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

Square is the name of the function; x is the name of it’s only arguments; and return x * x is the body of the function.

2). let in the case of declaring a variable is very limited in scope to the block, expression or statement that it is being used in.

var is a keyword that defined a variable globally, or locally to an entire function regardless of the block.

3). Purity is talking about whether or not a function will have side effects.

1 Like

1- By using the keyword “function” and parentheses that comes after it and the arguments can be placed in it or it has no arguments. Then the statement is to be placed between the brackets {}.

2- If you define a variable in a block with keyword “let”, it can’t be seen out of this block. It is local to this block. But if you define the variable by keyword “var”, it can’t be seen by the whole code(global) if it was 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.

2 Likes

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.

2 - The main difference is scoping rules. Variables declared by “var” keyword are scoped to the immediate function body, while “let” variables are scoped to the immediate enclosing block denoted by { }

3 - Pure Function is a function that does not depend on any state, or data change during a program’s execution rather it only depends on its input arguments and always returns the same result if the same arguments are passed

1 Like
  1. a function is created with an expression that starts with the keyword function.
  2. bindings with let are local bindings and var is in the global scope.
  3. is a 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:
With an expression that starts with the keyword ‘function’.

Example:

const sum = function(x, y) {
return x + y;
}
sum(4,9)
// 13

2:

When let and const are declared they are local to the block they were declared in.
For example, putting them in a loop will cause them to not be visible to the code
before or after said loop.

When var is used to declare a binding it can be used by code outside of that local block
and often it is globally available.

3:

It’s a value-producing function that doesn’t have any side effects and doesn’t rely on side effects from other code.
It always produces the same value.

1 Like
  1. A function is created with the keyword “function” and a paramater that defines the action to be executed when the function is invoked.

  2. let is local, as in applicable only to the block it is declared in and var is a global fuction that applies to the entie fuction regrdless of the block.

  3. A pure fuction is a fuction that gives a constant non vaiable output if given the same input. It produces not additional side effects.

1 Like
  1. is a regular binding where the value of the binding is a function.
    2.let is local to the block that they are declared in and cant be seen before or after the loop. as for var can be seen throught.
    3.is a function that always returns the same result if the same arguement passed.
1 Like
  1. You crate a function by using the function keyword.
  2. Let is used only on that local block, statement or expression, while var globally or locally to an entire function regardless of block scope.
  3. A pure function gives back the same the same input.
1 Like