- Functions are created by starting an expression with the word function .
- When using let it is local to the block that it is used in. Var is visible Throughout the whole function of the global scope .
- A pure function doesn’t have any side affects or use any side affects . A pure function will always produce the same results .
yes definitely see how that's confusing. And the link you left concerning let & var has helped. I need to deepen my understanding of function scope versus block scope verse global scope
much appreciated and as always...
Teamwork Makes The Dream Work
Fresh
-
You can create a function in Javascript by using an expression that starts with the keyword function. Define a name and within the body a set of parameters. Functions have a set of parameters (and can have none at all). Body of the function is the statement that will be called once it is executed and lastly you must always use the curly brackets to wrap the function.
-
The difference between VAR and LET in regards to scope is that LET are local to the block that they are declared in and cannot be seen by the whole program. VAR is global and the scope is the whole program and you can refer to these bindings whenever you want.
-
A pure function always returns the same result if the same arguments are passed in. It does not depend on any state or data, or change during a programs execution. It must only depend on its input arguments, also the function does not produce any side effects.
- How can you create a function in Javascript?
- By writing: var varname = function (parameter1,paramete2){ function code};
- function name(parameter1,parameter2){function code}
this function version is defines everywhere in code.
let h = a => a % 3; arrow notation - What is the difference between var and let in regards to scope?
variable declared with the let or const wor can only be seen inside the scope they are declared in. variables declared with var word can also be seen outside the scope the are created in. - What is a pure function?
A function that allways return the same number given the same input parameters. And also doesn¨t have any side effect. So it doesn¨t do anything else other than returning some data.
-
How can you create a function in Javascript?
by typing: function(); -
What is the difference between var and let in regards to scope?
var defines a variable globally (it can be local too) while let defines it locally for the block of the function -
What is a pure function?
a function that has no side effects and always provides the same output with the same input.
- By creating an expression using the function keyword, giving it a name and then creating a statement in the body of the function.
- The scope of Let is limited to the block where it is used, local. The scope for var can be the entire function they´re in or at global scope if the are not in a function.
- A Pure Function is function that will always return the same value, has no side-effects, and cannot be intervened by other variables.
To create a function it must first be named as shown below:
const test=function(num1, num2) {
CODE GOES HERE
};
In this case the function is called ‘test’. The key word function is then used and any parameters that need to be given to the function are defined in brackets. After this the program for the function is placed between braces.
Bindings created through the var keyword are visible throughout the whole function. If they are declared outside of a function they are treated as global and are seen by all parts of the program.
In contrast, bindings declared through the let keyword are only local to the block in which they are declared. So a let binding inside a loop is only visible inside that loop.
A pure function produces the same output each time for a specific set of inputs. Pure functions do not rely on other parts of the program and do not refer to global variables. Such functions can be tested independently of the main program.
I didn’t understand the function concept and this answer from @Jamal helped me. Answers 2 and 3 I understood fast.
- “A function in javascript is a regular binding where the value is determined from the code that follows, parameters are allowed and a value is returned. It starts with the keyword function, followed by optional parameters, body and a return value”.
- let is a local binding within the block that they are in whereas var is a global binding visible throughout the whole function.
- It is a function with no side effects and is determined by the input only.
1. How can you create a function in Javascript?
You can create a function in JavaScript by writing
function square (x) {
return x*x;
}
Or
const add = function(a){
return a+a
}
2. What is the difference between var and let in regards to scope?
var
would use the global scope and could be used anywhere in the program, if it’s not declared in another function. const
and let
are local, so they could only be used within the block that they’re declared in.
3. What is a pure function?
A pure function is a function that doesn’t produce, or depend upon, any side effects from other parts of the code.
I don’t really understand when you’d want to use a pure function or what a pure function would look like though.
There are a couple ways to create a function in javascript, one is creating an expression starting with the keyword function
followed by a set of parameters and the body, which contains the statements that are to be executed.
Another way is by declaration, a simpler way to code a function, by using the keyword function
at the beginning of a statement, for example:
function square(x) {return x * x;}
var
is global, as long as it is not in a function.
While let
is local to the block it is declared in.
It is a kind of value-producing function that has no side effects and does not depend on side effects from other codes.
- How can you create a function in Javascript?
function square (x) {
return x*x;
}
or
const square = function(x) {
return x * x;
};
-
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.
Old-style bindings, created with 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. -
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 has the pleasant property that, when called with the same arguments, it always produces the same value.
(Is a function that returns a random number with no side effects considered as pure?)
1**How can you create a function in Javascript?
function keywords+parameters(is not obligatory)+functoin body
2. What is the difference between var and let in regards to scope?
var is visible and let is not visible.
3. What is a pure function?
a function which always goes out with the same result.
How can you create a function in Javascript?
- A function is created with an expression that starts with the keyword function
- Functions have a 0 or multiple parameters.The body of the function excecutes the code of the function
What is the difference between var and let in regards to scope?
- let has a smaller scope only between {} where it is created which can be a loop inside a function
- var defined in the function will be ‘known’ within the function
What is a pure function?
- A pure function has the pleasant property that, when called with the same arguments,it always produces the same value (and doesn’t do anything else)
- a pure function it is not relying on side effects from other code
- how can you create a function in Javascript?
- There are three ways to create a function:
- Example 1:
// Define f to hold a function value
const f = function(a) {
console.log(a + 2); };
- Example 2:
// Declare g to be a function
function g(a, b) {
return a * b * 3.5; }
- Example 3:
// A less verbose function value
let h = a => a % 3;
- What is the difference between var and let in regards to scope?
-
Variables declared by
var
keyword are scoped to the immediate function body (hence the function scope) whilelet
variables are scoped to the immediate enclosing block denoted by{ }
(hence the block scope). -
Example of scope:
function run() {
var foo = "Foo";
let bar = "Bar";
console.log(foo, bar);
{
let baz = "Bazz";
console.log(baz);
}
console.log(baz); // ReferenceError
}
run();
- What is a pure function?
- A pure function has no outside variables involved while the function is performing its tasks. The function returns the same output when the same parameter is passed to it.
- Pure Function example:
function pure(arg) {
return 4 * arg;
}
console.log(pure(2));
3. What is a pure function?
Hello sir, here is an quick explain for pure function.
When the same input is passed every time, the function will return same output.
A pure function will have the following properties
It depends only on its own arguments.
It wont try to change variables out of its scope.
It doesn’t produce any side effects.
<script> let val1 = 6; let val2 = 4; function pure() { return val1 * val2; } document.write(pure()); </script>
You can read more about it here: What is a pure function in JavaScript?
Hope this gives you a clear picture of the subject.
If you have any more questions, please let us know so we can help you!
Carlos Z.
Excellent answers sir, well documented! Please keep them like that
Carlos Z.
1. How can you create a function in JavaScript?
You can create a function in JavaScript three ways:
1 - The function keyword, when used as an expression, can create a function value:
// define f to hold a function value
const f = function(a) {
console.log(a+2);
};
2- When used as a statement, it can be used to declare a binding and give it a function as its value:
function g(a,b){
return a * b * 3.5;
}
3-Arrow functions are a more abbreviated way to create functions:
let h = a => a % 3;
2. What is the difference between var and let in regards to scope?
Bindings declared with let and const are local to the block 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. Old-style bindings created with 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.
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. When called with the same arguments, it always produces the same value.
2. What is the difference between var and let in regards to scope?
Hello sir, could you please detail a little bit why their visible and not?
If you have any more questions, please let us know so we can help you!
Carlos Z.
Excellent answers sir, well documented! Please keep them like that
Carlos Z.
- How can you create a function in Javascript?
You can create a function in three different ways. When used as an expression, when used as a statement as well as with arrows as shown below.
let functionName = parameter => parameter * 2;
-
What is the difference between var and let in regards to scope?
The use of var allows the binding to be used(visible) throughout the whole function body.
Whereas, let and const can only be used locally within a block, like inside of a loop. Code before and after that loop cannot use(see) it. -
What is a pure function?
A pure function is a type of function that when called with the same arguments, it always produces the same value.