-
You can create a function by typing the keyword function followed by the name of your function.
-
var declaration is global, while let only works locally within the function from which it is declared
-
A function that does not produce any side effects but only returns values determined by its inputs.
-
You can create it with a word function (a) or you can make an arrow function (b).
a: function hiThere() {console.log(âHi Thereâ);}
b: hiThere = () => console.log(âHi There!â); -
"var" makes variable global or it makes is local to an entire function. âletâ makes scope limited to the block.
-
Pure function is a value-producing function that does not produce side effects and does not rely on other functions that produce side effects.
- How can you create a function in Javascript?
Declare it using the âfunctionâ reserved word. - What is the difference between var and let in regards to scope?
Scope. Var is global, Let tied to a block of code - What is a pure function?
A value producing function that has no side effects and does not rely on side effects from other code.
1. How can you create a function in Javascript?
There are three way to create a Javascript function.
Binding Function:
let sum = function (a, b) {
return a + b;
}
Declaration Function:
function sum(a, b) {
return a + b;
}
Arrow Function:
let sum = (a, b) => a + b;
2. What is the difference between var and let in regards to scope?
Bindings declared with let are visible just in block that they are declared in. On the other hand, bindings declared with var are visible throughout the entire function or the global scope if they are not in a function.
3. What is a pure function?
Is a function that return some value, donât have side effects and donât need any other information unless the parameters.
1. How can you create a function in JavaScript ?
A function is created with the keyword function, it has a set of parameters and a body which contains the statements to be executed when the function is called.
var funcitonName = function (parameters){
_____body
}
2. What is the difference between var and let in regards to scope?
The scope of var is bigger than let, it is global. For example in the case of a var binding, it can define a global **variable otherwise a let binding only exists in the block where is defined, it is local. **
3. What is a pure function?
It 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.
-
To create a function in JS, you need the keyword function, set of parameters, and a body.
//example const cube = function(x) { return x * x * x; };
-
The terms let and const are local variables with respect to the function scope.
The term var may be defined as a global or local variable, regardless of scope. -
A Pure Function doesnât have any dependencies outside its scope. So giving it the same parameters must yield to the same outcome every time.
- In Javascript you create a function by making a statement where the binding inside is composed by the word âfunctionâ and followed by round braces
example: const f = function (x){return x*x; } - Parameters and bindings declared by let are local and not visible from the outside. While parameters and bindigs declared by var end up in the nearest functionscope or in the global scope.
- 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.
- function name (x,y) {
return x * y;
} - Var is global and recognized by whole program. Let is local to that block only
- A function that relies on no other code and produces value
How can you create a function in Javascript?
function switchSquare(square) {
if (square == "#"){
square = " ";
} else {
square = "#";
}
return square;
}
What is the difference between var and let in regards to scope?
let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.
What is a pure function?
A pure function, when called with the same arguments, always produces the same value and doesnât do anything else. It has no side effects and doesnât rely on side effects from other code â for example, it doesnât read global bindings whose value might change.
-
How can you create a function in Javascript?
Create an expression that begins with the keyword function. The function will have 0 or more arguments and a body containing statements to be executed. If a value is to be returned to the program calling the function then there will be a âreturnâ statement with the value to be returned.
Note: arguemnts are placed in parentheses just after the keyword âfunctionâ and the body of the function is placed inside curly brackets. -
What is the difference between var and let in regards to scope?
Bindings declared with let are in fact local to the block that they are declared in whereas bindings declared with var in the main program are global in scope and bindings created with var in functions apply throughout the whole function. -
What is a pure function?
A pure function is a value producing function that has no side effects and doesnât rely on side effects from other code. When called with the same arguments, a pure function always produces the same value
function functionName(parameter1,âŚ)
{
code to be executed
}
2. Var - global scope unless defined in a function, let - local to the block they are declared in
3. Pure function is a function that doesnât return any side effects and does not rely on side effects from other code
- Function consist of:
- name of the function
- parameters - inside the parentheses
- body of the function - consist of statements to be executed when is the function called, is inside of curly braces
-
The âletâ bindings are local to the block, in which are they called.
The âvarâ bindings are visible globaly - through the whole function, in which are they called. -
Pure functionâs return value is determined only itâs input values, without observable side effects.
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 and a body, which contains the statements that are to be executed when the function is called eg
const square = function(z) {
return z * z;
};
console.log(square(10)); // â 100
2. What is the difference between var and let in regards to scope?
A key aspect in understanding functions is understanding scopes. Eachblock creates a new scope.
Parameters and bindings declared in a given scope are local and not visible from the outside. Bindings declared with var behave differentlyâthey end up in the nearest function scope or 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âfor example, it doesnât read global bindings whose value might change. A pure functionhasthepleasantpropertythat, whencalledwiththesame arguments, it always produces the same value (and doesnât do anything else).
How can you create a function in Javascript?
Begins with the keyword function. It can have no or multiple arguments. Body of the function is the statement that will be called once itâs executed.
What is the difference between var and let in regards to scope?
let are local to where they are declared and var is global.
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
Functions Reading Assignment
- Q.1 How can you create a function in Javascript?
- A function is created by binding a name equal to function, followed without a space, by curved brackets (or braces), which may contain zero or more parameter names. When calling, or using the function any value given will become the parameters given to the function and will therefore help determine the result of the function. Lastly, add the code that the function will use, between two curly brackets â{ }â.
- Q.2 What is the difference between var and let in regards to scope?
- If you declare a binding using the âletâ keyword it is local to the block of code that it is inside, for example, inside a loop.
The loop itself can see a binding immediately outside it. The binding has different values further outside the scope, that is, in nested loops, it can only see the innermost one. - If you use âvarâ to declare a binding (as used in any pre-2015 JavaScript), it will be visible throughout the function, or throughout the entire global scope if not inside a function.
- Using âconstâ to declare a binding follows the same rules as the âletâ keyword.
- If you declare a binding using the âletâ keyword it is local to the block of code that it is inside, for example, inside a loop.
The following code provides an example of the visibility of JavaScript bindings. âzâ is visible outside the loop, but not âyâ.
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);
// â 60
}
// y is not visible here
console.log(x + z);
// â 40
The following code shows that the first time the âhalveâ function was called,even though it changes âits own nâ to 50, the following output showed n equal to 10, in its outer scope.
const halve = function(n) {
return n / 2;
};
let n = 10;
console.log(halve(100));
// â 50
console.log(n);
// â 10
- Q.3 What is a pure function?
- A pure function only produces a value and does not produce any side-effects and does not rely on the side effects of any other code or the value of a global bindings, which might have changeable values.
- The benefit of a pure function is that if you call that it with any given parameters it will always produce the same result.
- How can you create a function in Javascript?
There are several ways of creating a function in Javascript. The common features of these methods revolve around:
- binding the function to its name and give its arguments e.g. function timesTwo(x);
- then following the declaration put the code block representing what the function is doing within curly brackets e.g.
function timesTwo(x) {
return 2 * x;
}
- What is the difference between var and let in regards to scope?
var defines global variables while let declares variables whose scope is limited to the block in which they are declared.
- What is a pure function?
A pure function does not have ay side effect.
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 and a body, which contains statements that are to be executed when the function is called.
What is the difference between var and let in regards to scope?
let is local to the block in which it is declared, inside of a loop. var is global if not in a function or visible throughout the whole function they appear in.
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.
- Function is created by using a keyword function().
- By using a var, variable can be created for local as well global access and let is used only for the local access.
- A pure function does not have any side effects and does not depend on side effects from other codes whose values can change.
A) const square = function(x) {
return x * x;
};
B) const square1 = (x) => { return x * x; };
OR
const square2 = x => x * x;
C) function square(x) {
return x * x;
}
-
let = local ; var = global
-
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 (and doesnât do anything else)
@Guactoshi, thank you for that link to the Eric Elliott video on pure functions! It helped me understand it better, and also some of the reasons we would be needing them.