Functions - Reading Assignment

1. How can you create a function in Javascript?
You can create a function by writing the function keyword to hold a function value:

const triValue(a, b, c) {
  return a * b * c;
}

You can declare a binding to give a function as a value:

function beFunction(f, g, h) {
  return f + g - h * 2;
}

Or you can write a more simple arrow function:

let plusTen = numb => numb + 10;

2. What is the difference between var and let in regards to scope?
A binding declared with let is local to the block, so you can not see it outside that block. On the other hand, a binding declared with var can be visible outside its block code.

3. What is a pure function? It is a function that has no side effects and doesn’t rely on side effects from other code.

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

  2. in regards to scope, var uses the global scope, and let only has local scope.

  3. a pure function is a value-producing function that not only has no side-effects but also doesn’t rely on side effects from other code.

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” are local to the block that they are declared in.
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?
It 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

How can you create a function in JavaScript?

  • The function keyword, when used as an expression, can create a function value:
    const f = function(a) {
    console.log(a+2);
    };
  • When used as a statement, it can be used to declare a binding and give it a function as its value:
    function g(a,b) {
    return a * b * 3.5;
    }
  • Arrow functions are a more abbreviated way to create functions:
    let h = a => a % 3;

What is the difference between var and let in regards to scope?
Bindings declared with let and const are local to the block 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. 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.

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. When called with the same arguments, it always produces the same value.

How can you create a function in Javascript?
  • declare a function:
    function myFunction(parameters){
    function code / statements
    return something (optional)
    }

    also function can be declared a variable, and if the function itself is not named, this is known as an anonymous function:
    const x = function(parameters){
    function code / statements
    return something (optional)
    }

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

  • within functions, the var variable is accessible to anywhere in the function block, even if created in separate code blocks within the function. The let variable is only accessible within the specific code block that it is defined in. Also, the ‘let’ variable will throw an error if declared twice, whereas the var variable is simply overwritten.

What is a pure function?

  • a pure function is one that a) only uses parameters that have been sent to is as part of the call b) produces a return value of some kind, and c) does not modify anything outside of the function code block (ie no side effects occur).

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 (). A function is executed when “something” calls it.
ex : function name(parameter1, parameter2, parameter3 ) { //the values given in function are called ‘Arguments’.
code to be executed
}

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

‘let’ gives you the privilege to declare variables that are limited in scope to the block. ‘var’ is a keyword which defines a variable globally regardless of block scope. (Global Window Object).

3 What is a pure function?

function priceAfterTax(productPrice) {
return (productPrice * 0.20) + productPrice;
} // It doesn’t depend on any external input and it doesn’t have any side effects. If you run this function with the same input 100,000,000 times it will always produce the same result.

var tax = 20;
function calculateTax(productPrice) {
return (productPrice * (tax/100)) + productPrice;
}// It produces side effect because it depends on an external tax variable! A pure function can not depend on outside variables,thus this function is impure.

  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? - var is visible throughout the whole function (or global) and let is visible to the block it is defined in (or local).

  3. What is a pure function? - a value producing code that has no side effects and does not rely on side effects from other code.

1. How can you create a function in Javascript?
Ans: Functions can be created using the function keyword. e.g.
function addNums(a,b) {
return a+b;
}

2. What is the difference between var and let in regards to scope?
Ans: let and const can be used to define a local scope variable which resides inside the function or in their “own little world”. Variables defined with var are usually available globally. It is worthwhile to mention that var has a disadvantage over const that a variable defined with var can be changed at any stage of program while variable defined with const will remain same throughout program.

3. What is a pure function?
Ans: Pure function is a special kind of function which doesn’t produce side-effects nor is dependent on side-effects from other blocks or parts of code. When called with same arguments it always returns same values.

How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function. Comprised of parameters 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.
What is the difference between var and let in regards to scope?
Var is visible within the whole function. Where as let is only visible within a specific loop.
What is a pure function?
Is a value producing function that neither produces or relies on side effects. When called with the same arguments always produces the same value and nothing else.

  1. A function is created by starting an expression with the keyword function
  2. A var binding has a scope throughout the function they were created in but let and const binding are restricted to the block they were created.
  3. A pure function doesn’t make use of side effect and does not create side effects
  1. How can you create a function in Javascript?
    Function yourVariableHere (argument 1, arg2, …) {
    parameter;
    another parameter;
    …;
    };
    Using your function
    yourVariableHere(arg1, arg2, …);

  2. What is the difference between var and let in regards to scope?
    Setting a binding with var inside of a block will still allow you to draw upon that binding outside of the block. Bindings set with let within a block can only be used within that block

  3. What is a pure function?
    A function that doesn’t produce any side effects nor is effected by the rest of the code around it. If given argument value x it will always produce a return value of x not y.

  1. Functions in javascript can be created in three different ways:
// As a value assignment
let sausageFactory = function ( orders = 1 ) {
    sausage = 1;
    return sausage * order;
}

// As a declaration that includes the function name
function sausageFactory2( orders = 1 ) {
    sausage = 1;
    return sausage * orders;
}

// As a short-hand value assignment via arrow function notation
let sausageFactory3 = (orders = 1) => { sausage = 1; return sausage * orders; }
  1. The difference between declaring bindings with var as opposed to let is that declaring something with var gives it global scope. let on the otherhand provides the binding only with local scope.

  2. Pure functions only rely upon their parameters to return a value and produce no side effects. This means they have consistent output given the same input.

  1. Either by writing
    functionName = function(x) {some code}; or by
    function functionName(x) {some code}
  2. If we create a var variable, it wil be visible as a global variable in the whole block, even if we created it in a loop for example. Let, on the other hand, will be regarded as a local variable, and could be accessed only in specific block, f.e. in that specific loop, and not outside of this loop
  3. It is a function that yields always the same results when provided with the same arguments, doesn’t depending on the context when it is used
  1. To create a function do:
    function nameoffunction(var1, var2) {
    //what you want the function to do
    }

  2. var and let are both Bindings, therefore they store and point to a value. The difference between them is that let is local to the block of code whereas var is “global”.

  3. A pure function has no side effects and doesnt rely on side effects from other code (eg var global bindings or other functions).

How can you create a function in Javascript? start your expression with the keyword function
What is the difference between var and let in regards to scope? var is global, let is local
What is a 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

  • There are several ways to create a function. The one I prefer is simply starting with the keyword “function” plus the arguments between parentheses and the body between curly braces.
  • var variables are accessible outside scopes as let variables are constrained within the scope they were created.
  • a Pure function is a function that doesn’t use side effects from other part of the program and also do not produce any side effects itself.

How can you create a function in Javascript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().The code to be executed, by the function, is placed inside curly brackets: {}function name(parameter1, parameter2, parameter3) {
code to be executed
}

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

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.

What is a pure function?

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.

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? : let is local to the block
What is a pure function? : A pure function, when called with the same arguments, always produces the same value (and doesn’t do anything else).

  1. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

  2. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block. Also, variables declared with let are not accessible before they are declared in their enclosing block.

  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 Like
  1. Keyword: function () => it may or may not be defined
  2. let = limited to a block; var = available globally
  3. It produces no side effects