Functions - Reading Assignment

Hi @Matoshi,

Great explanation of the different ways to create a function! :+1:

Q1 and 3 :ok_hand:

In terms of Q2, take a look at this post for some clarification. And if you would like to go even deeper, then try this post.

1 Like

1 - By creating an expression beginning with function() assigning parameters(if any), and a body with something to execute.

2 - let = declares a variable limited to the block, expression, or statement.
var = sets a variable globally

3 - A pure function is a value producing function without side effects from other code. When called with the same arguments it always generate the same results.

1 Like

Great job with explaining Q2. No One, but you make it clear for my, finally :slight_smile: Cheers

1 Like
  1. A function can be created in several ways: normal bindings, followed by the function keyword or arrow notation, or by a function definition.
  2. A binding created with let is only visible in the block where it is created, unlike one created with var.
  3. A pure function is a function which produces no side effects and which does not rely on any side effects.
1 Like

1.How can you create a function in JavaScript?

A function is created by expression that starts with the keyword function, followed by a name followed by parameters. The function name can contain letters, digits, underscore, and dollar signs (same as variables.)

Function name(parameters1, parameters2){
//Code to be executed
}

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

let variable inside block of code is LOCAL and can’t be defined outside the scope, var variable inside a scope is GLOBAL and can be defined outside the scope let’s see my example:

let IamGlobal = ‘someValue’

If(true){
var iamLocal = ‘someMorevalue’
Console.log(‘Iamglobal’)
Console.log(‘iamLocal’)
}

Console.log(‘iamLocal’)
Console.log(‘iamGlobal’)

Output:
someValue
someMoreValue
someMoreValue
someValue

3.What is a pure function?

It’s always has to return the same value for the same inputs also pure function has no side-effects.

1 Like
  • How can you create a function in Javascript?
  • A function is created with an expression that starts with the keyword function , after is a set of parameters and a body which contains the statement that are to be executed when the function is called.

  • What is the difference between var and let in regards to scope?
  • We have two types of scopes… Global and local. If we use let keyword, if we would like for it to be applied to a global scope we need to bind it outside on global scope (in document itself) before the function… Else if we use this let keyword inside of function (in local scope, its local block…) it will be applied only to that function and it will be “invisible” to the global scope. While var keyword is always visible for global scope unless it is used in a function.

  • What is a pure function
  • A pure function is a specific kind of value-producin function that has no side effect and it doesn’t rely of side effects from other code. If it is called with same arguments it always produces the same value (and doesn’t do anything else).
1 Like

Hi @Junte!

Q1 and 3 :ok_hand:

limited to the block — Yes
limited to the expression or statement — No

In terms of Q2, take a look at this post for some clarification. And if you would like to go a bit deeper, then try this post.

1 Like

Thanks for the feedback and the reading material Jon :grinning:

1 Like

How can you create a function in Javascript?

By defining its parameters and the caller.

What is the difference between var and let in regards to scope? Binding declared with let is valid only in the block where is declared, but a binding declared with var is visible on the whole 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. A pure function when called with the same arguments, it always produces the same value (and doesn’t do anything else).

1 Like
  1. function name(paramaters) {piece of code to be executed}
  2. let scope only inside the block whereas var scope also outside the block valid
  3. A function that does not create any side effects or rely on side effects
1 Like

Hi @cincinnato,

Q3 :+1:

There is more to creating a function than this… can you provide more detail?

In terms of Q2, take a look at this post for some clarification. And if you would like to go even deeper, then try this post.

1 Like

Hi again @sherlock! :slightly_smiling_face:

Q1 and 3 :ok_hand:

In terms of Q2, take a look at this post for some clarification. And if you would like to go even deeper, then try this post.

1 Like
  • How can you create a function in Javascript?
    you can use the keyword function as part of an expression that contains parameters and a body of statements to be carried out with the parameters defined (or not).
    A typical function structure could be
function functionName(parameter1,parameter2){ return(
parameter1 + parameter2);
};
//then invoke the function by defining the parameters
console.log(functionName(1, 2));
// result is 3
  • What is the difference between var and let in regards to scope?
    var defined bindings are globally recognized while let defined bindings are only locally recognized within the same block in which it was defined.

  • What is a pure function?
    A pure function produces no side effects and the same parameter input will always give the same output. It is independent of any other code outside of itself can be easily moved around within a program.

1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

2 Likes
  1. A function is created with an expression that starts with the keyword function. Functions may or may not have a set of parameters.

  2. var defines a variable globally while let refers it locally

  3. A pure function is a function that given the same input will always return the same output and it producess no side effects.

1 Like
  1. There are a few different ways, but the shortest and simplest one is;
    const counter = function() {…
  2. The scope of var is pre-2015 Javascript and defines the variable from the entire program, globally. Let focuses on the specific block it defines and limits the scope
  3. The return is based only off of its input values, doesn’t rely on other functions’ side effects and possesses no side effects
1 Like
  1. A function is created with an expression beginning with the keyword ‘function’ and has a set of parameters (x) and a body containing the statements to be executed which are always in {…} braces.

f(x) = functionName(parameters) {code to execute}

  1. Var scopes are visible in the entire function (global binding), and can be called from outside the function, while let scopes are only visible in local bindings.

  2. A pure function always produces the same value and it doesn’t read global bindings that may change value. It can only be substituted by its return value. It doesn’t have side effects and doesn’t rely from other functions’ side effects.

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

7 Functions

1 How can you create a function in Javascript?

When you use the keyword ‘function’ within an expression, you can create a function value. You can also create a function used as an statement that will declare a binding.

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

Let declared in a scope stays local and is not visible in a global scope. Var is different. It ends up in the nearest scope or in the global scope.

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

Bindings declared with “let” and “const” are local to the block, expression or statement that they are declared in, while bindings declared with “var” are visible throughout the whole function that they appear in—or throughout the global scope if they are not in a function.

3. What is a pure function?

A pure function is a value-producing function that when is called with the same arguments it always generate the same outputs. A pure function has no side effects and doesn’t rely on side effects from other code.

2 Likes