-
using keyword function(parameters){
perform function
return value
} -
let is local to block, function, environment etc.
var is global -
A pure function only returns a value, no sideeffects
Hi @Davide_Coltro!
Questions 1 and 3
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.
Hi @Henk1!
Questions 1 and 3
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
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.
Hi @lovreez!
Questions 1 and 3
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
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.
Great answers, @AidanH!
⌠especially your detailed explanation for Q2 with examples! I can see youâve really thought about it
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
Keep up the great work!
Hi @ADABOY!
Youâve got most of it
Now letâs fill in the gapsâŚ
Question 3
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.
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.
Hi @rbrownlow!
Youâve got most of it
Now letâs fill in the gapsâŚ
Question 3
Question1 âŚ
Nice examples of 2 ways to create a function
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.
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 .
Hi @Stas_Simon!
Question 2
Can you try and give more details for questions 1 and 3? Your answers are too generalâŚ
- 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
-
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 ). -
What is a pure function?
It has no side effects, so it returns the results provided and doesnât rely on other functions.
-
with const function = function(x){return x + 2;}
or function addtwo (x) {return x + 2;}
or const addtwo = x => x+2 -
Var is either global or local and let is local only I guess
-
a value-producing function with no side effects and that does not rely on side effect from previous code.
- 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. - 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.
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
At first that may seem like the easiest solution
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- 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.
-
An expression with the key word function. Then parameters and a body.
-
var is throughout the function and let is throughout a block.
-
It is a specific kind of value-producing function that has no side effects and doesnât rely on side effects from other code.
- 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} - âvarâ is defining a variable globally, whereas âletâ is defining a variable on a local scope.
- 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.
- 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; - 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.
- Pure function is that kind of function, which outputs just value, without side effects and are independent from rest of the program code.
- 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.
- 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.
- 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.
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.