Functions - Reading Assignment

1.How can you create a function in Javascript
We can create a function by using the function keyword, followed by whatever name we wish to give our function, its arguments and then the code within the function.

2.What is the difference between var and let in regards to scope?
For the keyword var, it is usually used for global variables, while the keyword let is for expressions within a function.

3.What is a pure function?
A pure function is a function without side effects. It is arguably more useful, in the sense that it will not affect any of the bindings outside of it’s function, which means that it can be used on its own, without having to worry that excessive usage might affect bindings that lie within other parts of the code.

1.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. The function body of a function created this way must always be wrapped in
braces, even when it consists of only a single statement.

2.Bindings declared with let and const are in fact 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. In pre-2015 JavaScript, only functions created 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, if they are not in a function.

3.A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope. Concretely, that means a pure function always returns the same result given same parameters. Its execution doesn’t depend on the state of the system. Pure functions are a pillar of functional programming.

  1. Function is created by with the keyword function. It should usually have a set of parameters and a body of statements to be executed when the function is called.

2.var allows you to define a variable globally, while let is used to define variables limited to a scope in a block.

3.Pure functions are value producing functions that have no side effects and do not rely on side effects from other code.

  1. Functions are created with the function keyword. A set of parameters usually follow that and then at the end you have the body. The body contains statements which are to be executed.

  2. Var ends up being visible globally while let will only be visible in the local scope.

  3. A pure function produces no side effect and doesn’t rely on side effects from other code. Given the same value, they always produce the same output.

How can you create a function in Javascript?

function Name(parameters seperated by comma’s) {code to execute}

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

var creates a global binding that can be seen thoughout a function, or throughout the global scope outside of functions, and let creates a local binding in the block it is created in.

What is a pure function?

A pure function has no side effects, and does not rely on side effects from other code.
It gives the same results at all times if fed with the same arguments.

  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function. However functions may have multiple parameters, such as values or not, like a side effect result.

  2. What is the difference between van and let in regards to scope?
    “let” with in a scope is a local binding with in 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.

  3. What is a “pure function”?
    A ‘pure function” will have the same input which returns the same output and produces no side effects.

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

    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.

    The function body of a function created this way must always be 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?

    var defines a variable globally, or locally to an entire function regardless of block.
    let can only declares variable in particular function block.

  3. What is a pure function?

    It will always return same output for particular input and it’s deterministic, they are predictable and stable. Pure function is easier to read, debug and test.

    It does not have any side effects, it doesn’t change anything outside of its boundary.
    A video helps to understand better, hope you can enjoy :smiley:

:tada: :tada: :tada: :tada: :tada: :tada: :tada: :tada: :tada: :tada:

1 Like
  1. How can you create a function in Javascript?
    You can create a function by first declaring it and give it arguments, if you so like. you can also decalre a function with no arguments. Example below:

function DisplayName(name) {
document.write(“My name is " + name +”.");
}

You invoke the function by typing the name of the function, followed by parathesis and if the function has arguments, you type the arguments inside the parathensis that follow the name of the function.
Example below:

DisplayName(“Hov”);

The output should be:
My name is Hov.

2.What is the difference between var and let in regards to scope?
Var uses the global scope if its not declared in a function or a block. let and const use block scoping and wont appear localy

  1. What is a pure function?
    A pure function is when a function wroks without side effects and that doesnt affect or is affected by other functions and values given to them before/afterhand.

1. How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function OR using the arrow (=>)

2. What is the difference between var and let in regards to scope? var (global), let/const (local)

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. A Function is a block of code set to execute a task
  2. Var means variable and will represent a piece of information which you can reference to as you build your program let represents a variable to the respective block of code. var can be over multiple statements or blocks.
  3. A pure function always produces the same results, independent of side effects.

1

A function is created with an expression that starts with the keyword function. Functions have a set of parameters and a body, which contrains the statements that are to be executed when the function is called.

2 let (and const) bindings are local (seen in a block of code, not outside it)
var bindings are global (seen everywhere)

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.

  1. How can you create a function in Javascript?
    There are 2 ways:
    // Define f to hold a function value
    const f = function(a) {
    console.log(a + 2);
    };
    // Declare g to be a function
    function g(a, b) {
    return a * b * 3.5;
    }

An optional way to use function is to use arrow function
// A less verbose function value
let h = a => a % 3;

  1. What is the difference between var and let in regards to scope?
    “let” has a more restricted scope while “var” has a wider 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 those inside of a loop, the code before and
    after the loop cannot “see” it. In pre-2015 JavaScript, only functions created
    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, if they are not in a function.
  2. 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. A function is a dedicated block of code designed to perform a particular task. Functions consist of function keyword, followed by a name followed by parentheses
    2.Let declares a variable limited to the block statement or expression.
    Var defines a variable in the code regardless of the block
  2. Pure function
    A specific kind of value- producing function that not only has no side effects, but doesn’t rely on side effects from other code

Functions - Reading Assignment

  1. How can you create a function in JavaScript?
    A function in JavaScript can be created by a function declaration or by a function expression.
    i) Function declaration.
    A function declaration is syntactically a statement which consists of the function keyword followed by:
    • The name of the function.
    • A list of parameters to the function enclosed in parentheses and separated by commas.
    • The JavaScript statements that define the function, enclosed in curly brackets, { }.

ii) Function expressions.
• Function expressions can be anonymous, that is, it does not have to be identified by a name. However, a name can be provided with a function expression and used inside the function to refer to it, or in a debugger to identify the function in stack traces:
Example: var factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1); };
Console.log(factorial(3));
• Function expressions can also be defined based on a condition. For example, function definition defines myFunc only if num equals 0:
var myFunc;
If (num === 0) {
myFunc = function(theObject) {
theObject.make = ‘Toyota’;
}
}

  1. What is the difference between var and let in regards to scope?
    • let in JavaScript allow us to declare variables that are limited in scope to the block, statement, or expression in which it is used.
    • var keyword defines a variable globally, or locally to an entire function regardless of block scope.

  2. What is a pure function?
    Pure function can be defined as follows:
    i) Pure function is the function that always returns the same result if the same arguments are passed in. However, it does not depend on any state, or data. It changes during a program’s execution and must only depend on its input arguments.
    ii) The function is not affected by network requests, input and output devices, or data mutation.

  1. Example: function add(a,b){
    return (a+b);
    }
    add(1,2);

also Arrow Functions

  1. let gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var . var is rather a keyword which defines a variable globally regardless of block scope .

  2. 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. 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. Let is local to the block they are declared in, so it can only be used within that block. var is global, so it can be used throughout the whole function it is declared in, or if not in a function, it can be used globally.

  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. How can you create a function in Javascript?
    Functions are created when an expression starts with the keyword function.Functions have a set of parameters and a body, which contains statements 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 are local to the block they are declared in, if it is created inside a loop, the code before and after the loop cannot "see"it. Using var is visible throughout the whole function they appear in or global scope, if they are not in a function.
  3. What is a pure function?
    Pure functions are value-producing functions that has no side effects, but also doesn’t rely on side effects from other code.
  1. const square = function(x) {
    return x * x;
    };
    or
    function square(x) {
    return x * x;
    }
    or
    const square = (x) => {return x * x;}; == const square = x => x * x; //if only one papameter

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.
The function body of a function created this way must always be wrapped in
braces, even when it consists of only a single statement.

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

  2. 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. How can you create a function in Javascript?  function(x){code}
2. What is the difference between var and let in regards to scope? var is accessible throughout the program while let is local to the code 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—forexample, 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).

How can you create a function in Javascript?

  • You can create a function just as you create a regular binding, but after the equal sign you write the word “function”, then in brackets you write the parameters, and finally you write the body of the function (the instructions). Something like this:

nameOfTheFunction = function(parameter1, parameter2, …){body};

// It is important to note that functions don’t need to have parameters

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

  • “var” is for global variables, while “let” is for local variables (restricted to the block where they are used)

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