Functions - Reading Assignment

  1. There are 3 possible ways to create a function:
    • const f=function(x) {}.
    • function f(x) {}.
    • f=x=>{}.
  2. “let” declares a variable limited to the block, statement, or expression on which it is used while “var” on the other hand defines a variable globally, or locally to an entire function reguardless of block.
  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. A function is a piece of code wrapped in a value. It starts with the name of the function which is also considered the keyword which you will lately use to recall the function. At this point the function it equal to the content of the function where there are statement that will be executed when the function is called and a set of parameters. The function body is contained in braces even when it contains a single statement.

    Name of the function keyword (parameters of our function) {
    content of the function all my Javascript code, the instruction, the code we want to happen
    }

  2. VAR and LET are name of functions and they both declares a variable.
    VAR is a valid for the entire function regardless of the block.
    LET is a variable limited to the block, statement, or expression in which it is used.

  3. A pure funcion does not have side effects so it is not determined unexpected results, will always give the same value given the same arguments. So you cannot change something in this fucntions, without changing the meaning . They are self-sufficient and for this reason, they can be used with confidence of not expecting unpredictable results because of them.

1 Like

*** 1. How can you create a function in Javascript?**
You can create functions by using the function command.

Example:
const hello = function(){
console.log(“Hello, how are you!?”);
}

In the example we defined a function of hello. When we call the function using “hello();”, the words “Hello, how are you!?” will appear in the console.

*** 2. What is the difference between var and let in regards to scope?**
Var is used to define the variable to be used in the larger scope. While Let is used to define the variable in the block, which limits it’s use.

*** 3. What is a pure function?**
A pure function has no side effects and does not rely on other code to get side effects. It always produces the same value.

1 Like
  1. keyword wrapped in braces which may or may not have set parameters.
  2. let is local to the block, var is declared globally if it is not in a function
  3. value producing function that does not produce or rely on side effects
  1. How can you create a function in Javascript?
    Usually using the function keyword (but we can also create “arrow” functions =>).

  2. What is the difference between var and let in regards to scope?
    While var can have a broader scope, let is only visible in the code block where it’s defined.

  3. What is a pure function?
    A function that does not have collateral effects, and it’s return value it’s always the same (for the same input parameters).

1 Like
  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 (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. What is the difference between var and let in regards to 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. With VAR you can use it outside of a block.

  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—for
example, 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

1 Like
  1. const sum = function (num1, num2){
    return num1 + num2;
    }
  2. Var can be overwritten, instead let can’t be. Var is accessible by the object window, instead let no.
  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?
Step 1: Create a binding (const) for a new function.
Step 2: Give the binding a name.
Step 3: Write function and define the parameters.

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

  • Let is only visible inside the block that they are created in and var will be visible outside of the block.

3. What is a pure function?

  • A pure function always produces the same value output given that the input data stays the same. Pure functions don’t produce any side effects.
1 Like
  1. Function is created with keyword function. After the keyword, there has to be a name of the function, and after that parameters. Ex. function hello (Name, Birthday) { }

  2. Let is not visible from the outside and variable that is created with let is local to the block or expression. Var can be used globally and can be seen from the outside.

  3. Pure function produces the same value every time(if arguments remain the same), and it has no side effects.

1 Like

Hi @DeCryptolorian!

Questions 1 and 3 :ok_hand:

Question 2 is really difficult. To be honest there really isn’t much of a difference between var and let. When you’re a beginner they are usually interchangeable. I always use let instead of var to be honest, and I’ve yet to run into any real problems with that approach :wink:

Take a deep breath, and see if this helps…

Similarities

A variable defined outside all of the program’s functions and code blocks (e.g. for loops, if statements etc.) has the same global scope whether it is defined with var or let . It can be accessed from anywhere else within the program.

A variable defined within a function body (but not also within any other functions or code blocks nested within that same function body) has the same local scope whether it is defined with var or let . It can be accessed from anywhere else within that same function (including from within any other functions and/or code blocks nested within it).

Differences

The differences are subtle and are a result of the following:

  • A variable defined with var has function scope
  • A variable defined with let has block scope

It takes a lot of thinking to wrap your head around what that actually translates to in terms of practical differences in their use, so don’t worry too much about the subtleties at this stage :wink:

5 Likes

Hi @dAnijboned!

Question 2 is really difficult. Have a look at this post — you may find it helpful.

2 Likes

Great, @Pendar! :muscle:
You’ve nailed this one! :smiley:

Yes… but var still has function scope, so if you declare a binding with var within a function then you can’t access it from outside of that function. This is only slightly different to the block scope of let and const.

To be honest there really isn’t much of a difference between var and let . When you’re a beginner they are usually interchangeable. I always use let instead of var to be honest, and I’ve yet to run into any real problems with that approach :wink:

A variable defined outside all of the program’s functions and code blocks, has the same global scope whether it is defined with var or let . It can be accessed from anywhere else within the program.

2 Likes

Hi @CryptoHero!

Question 3 :ok_hand:

It’s the function body (containing the code to be executed when the function is called) which is wrapped in the curly braces. The keyword function comes right at the beginning before the function body and also before the parentheses ( ) which, as you rightly say…

Question 2 is really difficult. You’re half way there :+1:
Have a look at this post — you may find it helpful.  :slightly_smiling_face:

1 Like

Hi Jon,
Yes it makes total sense. Thank you for the detailed explanation!

1 Like
  1. A function is a reusable mini program that is declared with the keyword function(thx @jon_m ). It can begin like this:

function addTheseTwo(x, y){
return x+y;
}
console.log(addTheseTwo(2, 3));

  1. In reference to scope, var is able to be used in a whole function while let variables are scoped to the immediate enclosing block ie {}.
    For ex:

function oneToFive(){

  for (let i = 1; i < 5; i++){
    console.log(i);
  }

  console.log(i);
  
};

oneToFive();

In this code once deployed you can see with let that once it hits 4 (the number just before it becomes >= to 5) that it gets caught with a reference error. This is because let will only work in the for loop and it will not print the console.log in the function properly. If we change the letter let to var like this:

function oneToFive(){

  for (var i = 1; i < 5; i++){
    console.log(i);
  }

  console.log(i);
  
};

oneToFive();

You can see it will print out the numbers 1-5 correctly because the scope of var is not just directed to the for loop but is global to the function(not whole code however just the function) making it able to run through the whole function.

  1. A pure function is a function that has a single responsibility and has no side effects. The function I used in 1) is a pure function because it follows those principles and with pure function like those they are easy to read and understand since they can only do one thing without any side effects.
1 Like
  1. by binding an expression to a value.

var number = function(x){
return x + x:
2. var is all encompassing and can be referenced outside of functions. Let is local and cannot.

  1. where the return value is only determined by its input values, without observable side effects. like console.log
  1. There are 2 main ways:
  • var f= function(a) {console.log(a*2)};
  • function(a,b) {console.log(a * b}
  1. A let binding is confined inside any function it may placed inside. A var binding can be seen outside any functions it may be placed inside.
  2. A pure function does not have any side effects. It also doesn’t rely on any side effects from any other code.
  1. Function is a a type of variable but more complicated.
  2. “var” is local to an enclosing function, 'let" is local to an enclosing block
  3. a function that only depends on input arguments

if you declare a binding with var within a function then you can’t access it from outside of that function.

always do it outside so it doesnt get locked!

  • 1. How can you create a function in Javascript? You can create a function by using the “function” keyword followed by the name of the function, followed by the parameters in parentheses. Functions are defined by bindings that link to blocks of code;
function priceAfterTax(productPrice) {return (productPrice * 0.20 + productPrice;}
  1. What is the difference between var and let in regards to scope? “var” is a pre 2015 keyword that creates global bindings visible to the entire code, “let” allows local bindings which are not visible to the rest of the code if they are nested between parentheses.
  2. What is a pure function? A specific value producing function that has no side effects and does not rely on side effects from other code. Impure functions reference values outside of their block.

A pure function:

function priceAfterTax(productPrice) {return (productPrice * 0.20 + productPrice;}

An impure function;

var = 20;

function calculateTax(productPrice) {return (productPrice * (tax/100)) + productPrice

}

This nice example is from https://medium.com/@jamesjefferyuk/javascript-what-are-pure-functions-4d4d5392d49c

1 Like