Functions - Reading Assignment

  • How can you create a function in Javascript?
    • Start w/keyword “function”
    • Set the parameters
    • End w/a “body”, the statements to be executed when the function is called:
      ex: function(set of parameters){body}
      • Parentheses n braces r required, just like in conditional statements
      • Functions can have multiple parameters or no parameters
      • Arrow functions r another way to create functions. The use the arrow operand:
        let variable = v => v * 3
  • What is the difference between var and let in regards to scope?
    • Var -
      • Usually has a global scope
    • Let -
      • Locally scoped to the block they’re declared in
      • Undefined, or not visible outside of their local block
  • 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”

@LORDKALVIN

1 Like

1- With an expression that starts with the keyword function.
2- var is used in the global scope and let is used in the local block. Var is also visible in the local scope, but let isn´t visible to the global scope.
3- Pure function is a special kind of value producing that has no side effects and don´t rely on side effects from other code.

1 Like

1. How can you create a function in Javascript?
by using the key word “function”. then, we need to give a name for the function. next comes the body of the function, describing the operation’s need to be executed.

2. What is the difference between var and let in regards to scope?
let is local to the block and can be seen just from inside of the block. var is global and can be used throughout the whole function or global, as long as it has a different name then the function name.

3. What is a pure function?
a pure function is a function that will always give the same value when given the same parameters.

1 Like
  1. How can you create a function in Javascript?
    A function is a regular binding where the value of the binding is a function. To create a function, it is done by using the keyword “function”. Functions have a set of parameters and a body. The body describes the operations needed to be executed.
  2. What is the difference between var and let in regards to scope?
    Let is a local scope to the block. It cannot see outside of it while var is a global scope. It can see the entire block of the function as long as it is not the same name as the function.
  3. What is a pure function?
    A pure function is a value production function that generates no side effects and does not depend on global binding.
1 Like
  1. You can create a function by using the function keyword as an expression to create a function value. You can also create a function when used as a statement, in declaring a binding and giving it a function as its value.

  2. 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.
    It can be said that a variable declared with var is defined throughout the program as compared to let. #ref# - https://www.geeksforgeeks.org/difference-between-var-and-let-in-javascript/

  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?

A function can be defined in two ways shown below. I will create a function called myFunc below in two of the ways.

Method 1: Using a variable or binding to store the function definition:

const myFunc = function() { function body here }

Method 2: Using name of the function after function keyword:

function myFunc() { // function body here }

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

variables or bindings created with the var keyword are visible to the global scope they are created in so if they’re created in a function then they are visible to a function. However, variables created using let or const are local only to the block of code they are created in. So if a variable is created in an if block or in a for or while loop block using let or const then it cannot be accessed outside of this blocks.

So let and const essentially allow the programmer to create a scope for a variables. Before these keywords functions could only create new scopes.

3. What is a pure function?

A function that produces no side effects and doesn’t rely upon the values of global variables elsewhere in the code outside the function whose values might change.
In other words, a pure function is a self contained function whose return value depends on the parameters passed to it if any and nothing else.

1 Like

By using the keyword “function”

The ‘var’ keyword has a global scope meaning it can be accessed in all parts of the program whereas the ‘let’ keyword has a local scope meaning that it can only be used in that specific part of the program

A pure function takes in the same variable and returns the same variable as output without side effects.

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

  2. What is the difference between var and let in regards to scope?
    let bindings are local to the block, so if you create a binding inside of a loop, the code before and after the loop cannot see it. Whereas for the var binding, the binding is visible throughout the global scope.

  3. What is a pure function?
    A pure function is a value producing function that has no side effects and does rely on other side effects, and when called with the same argument, it always produces the same value.

1 Like
  1. How can you create a function in Javascript?
    function name (parameter1, parameter 2, …) {body with the code to be executed}
  2. What is the difference between var and let in regards to scope?
    var is declared for the whole program and let is only declared localy to the block where it is used
  3. What is a pure function?
    A pure function is a function that behaves in a predictable way depending on it’s input parameters. In other words it is not influenced by global variables.
1 Like
  1. How can you create a function in Javascript?
    Functions are created using the function keyword followed by an expression ( a name or binding for the function and a set of parenthesis)

  2. What is the difference between var and let in regards to scope?
    let and const keywords are local to its block or function, the code before or after the block cannot see it. Where as the var keyword has global scope.

  3. What is a pure function?
    A pure function does not produce any side effects, it will always produce the same value if the same arguments are passed through, it is not dependent or affected by external variables.

1 Like
  1. Programmers in JS must define the function before it can be used. Once defined and invoked the function can be used over repeatedly saving the programmers time and improving efficiency.These functions execute certain actions and perform tasks by using functional keywords. These are enclosed in braces that execute as a result of being called.
  2. The differences between keywords “var” and “let” have to do with their bindings and scopes within local or global functions of a program. Bindings declared with keywords “let” and “const” are considered local and the computer program addresses them within the function that they are declared. Their roles (actions and tasks) remain within the declared usage and cannot act on pieces of code in different parts of a program. Whereas, keywords such as “var” have bindings and scopes that allow full access to all and the entirety of its programming. Var has more flexibility and at times is used in a local context of a program.
  3. A pure function in programming has return values that are identical for identical arguments and can be a computational analogue of a mathematical function. Pure functions do not interact with global states and have no side effects or variations.
1 Like
  1. You can create functions in Javascript by using Function FunctionName(ex: getAge), give it a parameter/parameters within parentheses, then the body of the function within curly brackets.
  2. The let keyword alongside const is local to the section of code that they are declared in. The bindings that use the let and const will not be noticed by the functions/blocks of code, outside of the one they were declared in. Bindings that use the var keyword are noticeable within the entire function they were declared in. var bindings will be visible globally if not declared within any function.
  3. A pure function is a function that does not create any side effects and also does not rely on side effects from other code.
1 Like

Q.1.How can you create a function in Javascript?
A.1. A function is created with an expression that starts with the keyword
function. Functions have a set of parameters (in a set of parens) and a body
with braces {}. The body contains the statements that are to be executed when
the function is called.

Q.2.What is the difference between var and let in regards to scope?
A.2.the var keyword, is visible throughout the whole function that appear in—or
throughout the global scope, if they are not in a function.

Bindings declared with let are local to the block that they
are declared in. For example, if a let inside of a loop is used,
the code before and after the loop cannot “see” it.

Q.3.What is a pure function?
A.3. Is a specific kind of value-producing function that has no side effects
and does not rely on side effects from other code.
a. does not rely on global bindings whose values might change.
b. when called with the same arguments, always produces the same value
c. a call to a pure a function can be substituted by its return value without changing the
meaning of the code.
d. After being called a pure function, will works in any context.

2 Likes
  1. How can you create a function in Javascript?
    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. 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?
    A binding declared with let is local to the block it is declared in. A binding declared with var is visible throughout the whole function or throughout the global scope, if it is not in a function.

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

2 Likes
  1. A function is a regular binding where the value of the binding is a function. Its created with an expression that starts with the keyword function.

  2. Bindings that are declared with “let” are local to the block that they’re declared in. If its created in a loop, the code before and after the loop cannot see it.

  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. A function is created through the keyword function, then the name of the function, and then the parameters inside (), and then the body {} that contains the statements

  2. Var can be globally, let are variables that are limited to a block

  3. A pure function is a value producing function with no side effects, and has no reliance on other code with side effects

2 Likes
  1. How can you create a function in Javascript?
    function(/parameters/) {/body = statement/}

  2. What is the difference between var and let in regards to scope?
    let declares local bindings. When declared inside a loop, the code before and after the loop cannot “see” the binding.
    Var declares a global binding which is valid throughout the function or even throughout the code if not declared within a function.

  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 Like

1 By using the keyword function then giving it a parameter ( ) and then a body { }.

2let and const use block scoping, they won’t appear locally. Var uses the global scope if they are not declared in a function.

3.It doesn’t produce or rely on side effects, avoids reading global bindings.

1 Like
  1. How can you create a function in Javascript?
    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. 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.

  3. What is a pure function?
    Pure Function is a function (a block of code ) that always returns the same result if the same arguments are passed.

1 Like
  1. How can you create a function in Javascript?
function myFunction (a){
  return a + 100;
}
  1. What is the difference between var and let in regards to scope?
    let will produce an error if the variable isn’t declared before calling it. var will return undefined when called before declaration.

  2. What is a pure function?
    A pure function is a function where the return value is only determined by its input values, without observable side effects. This is how functions in math work: Math.cos(x) will, for the same value of x , always return the same result. Computing it does not change x .

1 Like