-
How can you create a function in Javascript?
Declare the keyword function followed by the name of the function. The function may receive parameters, which are passed inside brackets. The body of the function is wrapped in curly braces and contains any code to be executed when the function is called. If the function produces a value, rather than a side effect, then a ‘return’ statement is used to return the value…
function myFunction(param1 param2){
var rtn = 0;
rtn = param1 * param2;
return rtn;
{ -
What is the difference between var and let in regards to scope?
A var binding created anywhere within a function exists within the scope of, and is visible throughout the function, whereas a let binding only exists within the scope of the block that created it. An example would be a let binding that was created within an ‘if’ block, which would not be visible before or after the block. -
What is a pure function?
A pure function is one that produces a value and does not produce side effects or is reliant on side effects from other code.
Question 1
Functions can be created in JavaScript in the following 2 ways:
- The
function
keyword can be used to create a function expression.
A variable is defined in the usual way and the function expression is assigned (using the=
operator) to the variable’s name as its value. The function expression itself starts with thefunction
keyword followed by parentheses, which contain the function’s parameters (or are left empty if there are no parameters). The function body — a code block wrapped in curly braces — comes after the parentheses and contains the statements to be executed when the function is called. These statements may or may not include code that executes side effects, such asconsole.log()
. If the function needs to return a value, then this is achieved by creating a statement with thereturn
keyword followed by an expression which evaluates to the value to be returned. A semicolon is placed after the closing curly brace.
const name = function(parameter1, parameter2) {
// function body
return parameter1 * parameter2;
};
- Alternateively, the
function
keyword can be used to create a function declaration.
Instead of defining a variable with the keywordconst
orlet
, thefunction
keyword opens a statement which contains the name of the variable the function is assigned to, followed by parentheses (containing any parameters) and finally the function body. The function body has the same features and characteristics as the one in the function expression, except no semicolon is required after the closing curly brace.
function name(parameter1, parameter2) {
// function body
return parameter1 * parameter2;
}
With both methods, the function is called using the variable name it has been assigned to, followed by parentheses. The parentheses contain any arguments fed into the function (to serve as its parameters) or are left empty if the function has no parameters.
name(argument1, argument2);
Question 2
A variable defined with var
has function scope. This means that if the variable is defined within a function, then it can be accessed from anywhere within that same function, but not from outside the function. If the variable is defined outside all of the program’s functions, then it has global scope, meaning that it can be accessed from anywhere within the program.
A variable defined with let
has block scope. This means that if the variable is defined within a code block, then it can only be accessed from anywhere within that same code block, but not from outside the code block. If the variable is defined outside all of the program’s code blocks, then it will have global scope, meaning that it can be accessed from anywhere within the program.
It therefore follows that:
-
A variable defined outside all of the program’s functions and code blocks has the same global scope whether it is defined with
var
orlet
. It can be accessed from anywhere else within the program. -
A variable defined outside all of the program’s functions, but within the curly braces of an
if
statement, or loop, can be accessed from anywhere within the program if it is defined withvar
. However, if it is defined withlet
it can only be accessed from within the same code block (including from within any functions and other code blocks nested within it). -
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
orlet
. It can be accessed from anywhere else within that same function (including from within any other functions and/or code blocks nested within it). -
A variable defined within a function body (and also within the curly braces of an
if
statement, or loop, nested within that same function body) can be accessed from anywhere within the function if it is defined withvar
. However, if it is defined withlet
it can only be accessed from within the same code block (including from within any functions and/or other code blocks nested within it).
Question 3
A pure function has two main characteristics:
-
It is deterministic, meaning that it will always return the same value when fed the same arguments. This is because it is a closed system, and does not depend on any side effects or other external variables.
-
It does not produce any side effects, and only returns a value.
-
How can you create a function in Javascript?
A function is created with the keyword “function”. They have parameters and a body with statements (to be executed, wrapped in braces). -
What is the difference between var and let in regards to scope?
“let” is a local for the block they are declared in. “var” is global and can be used on the whole function it is declared in (or globally). -
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.
-
The syntax to define functions in Javascript is as follows:
function functionname(var1, var2, etc)
{
code for action to be taken
} -
var == function scoped, defined throughout the program
let == block scoped -
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 produces the same value (and doesn’t do anything else).
- By using " function()" or “>=” like normal binding
- Var is pre 2015 Javascript, it is global binding; Let is post 2015 Javascript, it is local binding.
- Pure function has no side effect and does not rely on side effect from other part of the code.
-
How can you create a function in Javascript?
A function definition is a regular binding where the value of the binding is a function.
const square = function(x) {
return x * x;
};
2. 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 in3. 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
-
How can you create a function in Javascript?
A function is created in JavaScript with an expression using the function keyword. -
What is the difference between var and let in regards to scope?
They differ because let is visible only within the local scope of the function block it was declared in while var is visible throughout the entire function. -
What is a pure function?
A pure function only produces a value, has no side effects and does not rely on side effects from other code.
1.How can you create a function in Javascript?
function LoveSteak ();{
}
2.What is the difference between var and let in regards to scope?
var - defines Globaly
let - limited to the scope
3.What is a pure function?
It has no side effects and doesnt rely on side effects from other code.
1a. const name = function(a){ ;};
b. function name(a){ ;}
c. let name = a => ;
-
var applies globally even if it was created inside of a function, while let will only be defined inside the block it was created.
-
A pure function gives a value with no side effects and doesn’t rely on other side effects to function.
1 A function is created with an expression that starts with the keyword function.
2 let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. var keyword defines a variable globally, or locally to an entire function regardless of block scope.
3 A pure function is a function that not only has no side effects, but it also doesn’t rely on side effects from other code. When it is called with the same arguments, it will always return the same result.
- How can you create a function in Javascript?
An expression that starts with function, following by parameters (or not), with instructions between the curly brackets that follow
- What is the difference between var and let in regards to scope?
Let can only be accessed inside the scope it was declared. Var can be used anywhere outside of the scope as well
- What is a pure function?
A function that has no side effects and is also deterministic
How can you create a function in Javascript?
Function is created with an expression that starts with the keyword function
function type_name(parameter1, parameter2){
//code to be executed
}
document.write(type_name);
What is the difference between var and let in regards to scope?
Var defines a variable globally, or locally to an entire function regardless of block scope.
Let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used
What is a pure function?
Is a specific kind of value producing function that not only has no side effect but also doesn’t rely on side effects from other code
-
How can you create a function in Javascript?
With an expression that starts with the keyword function. Then parameters(x,y)
and body {instructions} -
What is the difference between var and let in regards to scope?
Var is function scoped and let is block scoped. -
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.
-
How can you create a function in Javascript?
By writing var myvariable = funct(x){some code here} -
What is the difference between var and let in regards to scope?
Let will only be visible to code within it’s bindings whereas var will be visible globally. -
What is a pure function?
One with no side effects.
- How can you create a function in Javascript?
Typing the keyword function followed by parenthesis with multiple parameters or none of them inside. After that and always inside braces comes the function body.
- What is the difference between var and let in regards to scope?
var is always visible throughout a function or global when is outside a function. However, let or const are only local so their scope is the block they are declared in.
- What is a pure function?
It is a kind of function that produces a value and has no side effects. It doesn’t rely on side effects from other code either. Moreover, when called with the same arguments, it always throw the same value and doesn’t do anything else.
- How can you create a function in Javascript?
by using the word “function” and naming the function then setting the parameters then the body of statements to be executed. - What is the difference between var and let in regards to scope?
var is set globally in the code so anything after it is set will see and use it where as a let is only visibly used in a local scope inside a loop and will not be used after the loop. - What is a pure function?
It has no side effects and will not rely on side effects from other code, it will always produce the same value.
- How can you create a function in Javascript?
Like a regular binding where the value is the definition of the function - What is the difference between var and let in regards to scope?
var is used for global scope, while let sets the scope to the local block - What is a pure function?
A function without side effects, and which when called with the same arguments always produces the same results.
With the expression “function” followed by a “name”, followed by parentheses
Let is local to the block that they are declared in; var is visible throughout the global scope
Do not depend on external variables and do not create any noticeable 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. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement.
2. 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. In pre-2015 JavaScript, only functions created new scopes, so 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—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 (and doesn’t do anything else). A call to such a function can be substituted by its return value without changing the meaning of the code.
-
How can you create a function in Javascript?
is a function given wrapped in the parameters then the code to execute is enclosed with the parentheses -
What is the difference between var and let in regards to scope?
the var is with the whole function of the particular program where as let is only visible to the block that it is declared in -
What is a pure function?
A pure function will always return the same result if the same results are passed in