Functions - Reading Assignment

  1. using keyword function(parameters){
    perform function
    return value
    }

  2. let is local to block, function, environment etc.
    var is global

  3. A pure function only returns a value, no sideeffects

1 Like

Hi @Davide_Coltro!

Questions 1 and 3 :+1:

Not really…both var and let can have their values reassigned, it’s const that can’t be.

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

2 Likes

Hi @Henk1!

Questions 1 and 3 :+1:

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.

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

1 Like

Hi @lovreez!

Questions 1 and 3 :ok_hand:

Kind of… 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.

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

1 Like

Great answers, @AidanH! :smiley:
… especially your detailed explanation for Q2 with examples! I can see you’ve really thought about it :+1:

Only one thing to confirm really:

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.

But I think you’ve already understood that :ok_hand:

Keep up the great work! :muscle:

2 Likes

Hi @ADABOY!

You’ve got most of it :ok_hand:
Now let’s fill in the gaps…

Question 3 :ok_hand:

Question1 …

Don’t forget the closing curly brace.
The statement in your function body should end with a semicolon, not a colon.

Not really… Question 2 is really difficult. Have a look at this post — it should help clear some things up. :slightly_smiling_face:

Also check out @AidanH’s post here— his explanation is really good and he’s given a great illustrative example of the differences between var and let when used within a function.

1 Like

Hi @rbrownlow!

You’ve got most of it :ok_hand:
Now let’s fill in the gaps…

Question 3 :ok_hand:

Question1 …

Nice examples of 2 ways to create a function :+1:
Just a couple of things tp point out… the function name is missing in your second example:

function name(a,b) {
console.log(a * b);     // and don’t forget the closing bracket here
}

No … Question 2 is really difficult. Have a look at this post — it should help clear some things up. :slightly_smiling_face:

Also check out @AidanH’s post here— his explanation is really good and he’s given a great illustrative example of the differences between var and let when used within a function .

1 Like

Hi @Stas_Simon!

Question 2 :+1:

Can you try and give more details for questions 1 and 3? Your answers are too general… :wink:

1 Like
  1. How can you create a function in Javascript?
    A function is created as such:
//Define function
function(parameters){
//this area is where you shall perform a function
return code to be executed
}
//output of funtion

//eg:
function mult(a,b){
    return a*b
}
console.log(mult(5,2));
//10
  1. What is the difference between var and let in regards to scope?
    var is a function scope(can be called globally), but let is a block scope(can’t be recalled globally/outside the function ).

  2. What is a pure function?
    It has no side effects, so it returns the results provided and doesn’t rely on other functions.

1 Like
  1. with const function = function(x){return x + 2;}
    or function addtwo (x) {return x + 2;}
    or const addtwo = x => x+2

  2. Var is either global or local and let is local only I guess

  3. a value-producing function with no side effects and that does not rely on side effect from previous code.

1 Like
  1. A function is a piece of program wrapped in a value. where the value
    given to the variable happens to be a function. It is a tool to structure larger programs.
  2. Pure function doesn’t have side effects and doesn’t depend on anything but what’s inside of it.
    That is the description from the Javascript book. I haven’t seen anything else.
1 Like

A: A function is created with an expression that starts with the keyword function

A: 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. the scope is
the whole program—you can refer to such bindings wherever you want

A:it avoids global bindings by keeping the same input and returning the same outout with no side effects

1 Like

At first that may seem like the easiest solution :wink:

But, most programs are much much bigger and much more complex than the ones we’ve been working on in this course.

We should always try to keep the scope of our variables limited to what’s necessary. Otherwise the global namespace can get polluted, and there is less overall control of the program which can make it easier for bugs to creep in.

Also, by keeping our variables limited to the scope they are designed to be used in, we make our program more readable and easier for other developers to understand and work with.

1 Like

1- let bindingA = function (a) {a * “random string”; return a; };
let bindingA = a => a * “random string”;
function bindingA (a) {a * “random string”; return a;};

2- var bindings are global unless declared within a function and let bindings are local and can only be seen by the block they are declared in or within the function it is declared in.

3- A pure function is a function that has no side effects and will always produce the same result if given the same arguments.

1 Like
  1. An expression with the key word function. Then parameters and a body.

  2. var is throughout the function and let is throughout a block.

  3. It is a specific kind of value-producing function that has no side effects and doesn’t rely on side effects from other code.

1 Like
  1. A function in JavaScript is an expression containing the word function or (=>) and where the binding value(s) (parameter(s)) is a function. A function is designed to perform a task within its body and the rules of that function cannot be used outside of its environment.
    There are three ways of writing a function:
    a) Definition Function: keyword bindingName = function(parameter1, parameter2, parameter3){body}
    b) Definition Function: function bindingName(parameter1, parameter2, parameter3){body}
    c) Arrow Function: keyword bindingName = (parameter1, parameter2, parameter3) => {body}
  2. ‘var’ is defining a variable globally, whereas ‘let’ is defining a variable on a local scope.
  3. A pure function always produces the same value and does not have any side-effects or rely on the side-effects of any other code. It does not read global bindings.
1 Like
  1. There is a 3 ways to create it:
    a. Define f to hold a function value
    const f = function( ) { };
    b. Declare f to be a function:
    function f( ) { };
    c. And the arrow method:
    let f = a => a * 3;
  2. Let is a variable defined and used only inside the scope of the function, it’s not possible to access it from outside. Variables defined by var could be reached in the whole code, including functions.
  3. Pure function is that kind of function, which outputs just value, without side effects and are independent from rest of the program code.
1 Like
  1. How can you create a function in JavaScript?

A function can consist of parameter or have none. A function is a regular binding value with set parameters if it has any, a body that is wrapped in braces no matter the size of the statement. Functions can also consist of values. Functions without key words are returned as undefined. The caller of a function is given this value by behaving like regular bindings and not based on the code in the function.

  1. What is the difference between var and let in regard to scope?

A binding like var become visible through the whole function, in contrast let is only seen in the block they are declared in.

  1. What is a pure function?

I a specific of value producing function with no side effects, nor does it relay on the side effects from code. When using the same arguments pure functions produce the same values.

1 Like

Great answers, @turner36t!

Just one thing to add…

While this is true, bindings declared with let can also have global scope if they are declared outside of all code blocks and functions.

1 Like

Hi @AF90,

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.