- with the “function” keyword or the arrow " => "
- var will end up in the nearest or global scope, where let will sat in local scope
- is a function that doesn’t rely on other code to work properly, when called with the same argument it always produces the same value (and doesn’t do anything else)
- 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 (in this case, only x) 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.
- What is the difference between var and let in regards to scope?
Bindings declared with let are local to the block that they are declared in. Bindings declared with var are global to the whole program.
- 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 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. -
What is the difference between var and let in regards to scope?
The scope of let is limited to the block while the scope of var is global. -
What is a pure function?
A pure function is a specific kind of value-producing function that does not have any side effects and does not rely on side effects from other code.
- The function keyword, when used as an expression, can create a function value. When used as
a statement, it can be used to declare a binding and give it a function as its
value. - Var can be globally aplicable, while let is only called locally.
3.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?
use the keyword function, then the set of parameters, followed by the code to be executed.
funtion () {//code to be executed}; -
What is the difference between var and let in regards to scope?
Let has local binding when the function is called. Var can be local or global binding depending on its scope. -
What is a pure function?
Given the same input, it will always return the same output and produces no side effects.
1. How can you create a function in Javascript?
One way to create a function is with a function binding in which a function definition is a regular binding where the value of the binding is a function. A function is created with an expression that starts with the keyword function. Functions have a set of parameters (params here), and a body which contains the statements that are to be executed when the function is called. The function body when created this way must be wrapped in braces. A function binding is defined once and never changed.
A different way to create a function binding and the more common way is a function declaration, in which the function keyword is used at the start of a statement. Function declarations are not part of their scope and can be used by all the code in that scope.
Arrow functions are essentially the same as function declarations but use a different syntax. When you pass one param it does not need to be enclosed in parenthesis and the => implies an implicit return so the return statement is not necessary.
// function as a value (as arrow function):
let greeting = name => {
console.log("Hello " + name + "!")
}
greeting("Rose")
// Hello Rose!
// function declaration :
// This function gets the minimum of two numbers when passed in.
function getMin(numOne, numTwo) {
return Math.min(numOne, numTwo)
}
console.log(getMin(10, 4))
// 4
// Arrow Function, function declaration
const getMin = (numOne, numTwo) => {
Math.min(numOne, numTwo)
}
console.log(getMin(10, 4))
// 4
2. What is the difference between var and let in regards to scope?
Bindings declared with let and const are local to the code block of the function they are declared in. The var keyword is visible throughout the whole function as well as the global scope if they are not in the function.
let x = 10;
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);
// >> 60
}
// y is not visible here
console.log(x + z);
// >> 40
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 does not rely on side effects from other code. Given the same input it will always return the same output.
Read Chapter 3 in the book called Functions .
It is of course beneficial if you understand and read the entire chapter, however, feel free to skip the following sections because we will not use those Javascript features in this course (Arrow Functions, Closure, Growing Functions) .,
Think about the following questions while you are reading:
- How can you create a function in Javascript? | There are two ways, I think, and I will put examples here:
Function fn1(){
(insert function here)
}
const afunction = function(){
(insert function here)
}
-
What is the difference between var and let in regards to scope? | let is local to the block that they are declared in, while var variables are throughout the whole function.
-
What is a pure function? | A pure function is a deterministic function, meaning that when a same input is passed every time, the function will return the same output.
-
You can create a function in JavaScript by setting the function name, which usually determines the act of the function, and then wrapping one or more values in parentheses.
-
Var is visible in the so called “global” scope while let is only visible in the scope in its own block of code.
-
A pure function is a function which given the same input will always return the same output . Pure functions won’t produce side effects.
-
A function is created with an expression that starts with the keyword function
. Functions have a set of parameters or or no parameters at all. -
var and let* are both used for variable declaration in javascript but the difference between them is that var is function scoped and *let is block scoped.
-
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 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 main difference is the scope difference, while let can be only available inside the scope it’s declared, like in for loop, var can be accessed outside the loop for.
-
Pure functions are functions that accept an input and returns a value without modifying any data outside its scope(Side Effects). Its output or return value must depend on the input/arguments and pure functions must return a value.
-
How can you create a function in Javascript?
A. A function is created with an expression that starts with the keyword function
. Functions have a set of parameters (in this case, only x) and a body, which
contains the statements that are to be executed when the function is called. -
What is the difference between var and let in regards to scope?
A. Let has local scope and restricted to the block and var has global scope and is for the program. -
What is a pure function?
A.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.
-
A function is created by writing the word “function” at the begining and following up the name of the function. We can also set parameters for this function.
-
With let you can designate variables that are limited to the block, expression or statement. Var defines a variable globally.
-
The return value is determined by the input values.
- How can you create a function in Javascript?
Expression:const f = function(a) { console.log(a + 2); }
Declaration:function i(d) { console.log("My name is " + d); }
- What is the difference between var and let in regards to scope?
Var
is global (unless a function)
Let
is local to the block in which they were created. - What is a pure function?
Has no side effects and doesn’t rely on other side effects. Given the same input, it will always produce the same output(s).
-
You can create a function by writing an expression that starts with the keyword function. You also need to define its parameters and its body thtat need to be executed when the function is called. It looks something like this example: const square=function(x){return x*x;}
-
Let
s scope is limited locally, whereas var
s scope applies globally. -
A pure functions is a specific kind of value-producing function that has no side effects and does not rely on side effects from other code. So, when called with the same arguments, it always produces the same value.
1.) By using the expression/keyword function. A function has a parameter and a body.
2.) let is local → “exists” only in its function/block
var is global → visible throughout all code if no in a function
3.) a pure function returns a value. Has no Side effect and does not rely on another side effect.
- you can create a function by using the keyword function, defining the parameters and writing the body statement which is executed when the function is called.
- Bindings created with var are visible to the whole script, so are said to have a global scope, where as the bindings created with let are only visible within the block they are defined in, so are said to have local scope.
- A pure function is a function that doesnt return any side effects, or rely on side effects from previously called function.
Q: How can you create a function in Javascript?
A: const a = function(x,y) {
return x + y ;
};
Q: What is the difference between var and let in regards to scope?
A: Let command is local to the block that is declared in. So, if we create it inside a loop it will not be visible before and after the loop. Var commands are visible throughout the function and appear the the global scope.
Q: What is a pure function?
A: It is a function that has no side effects and neither relies on them.
- How can you create a function in Javascript?
A function definition is a regular binding where the value of the binding is
a function.
For example:
// Define f to hold a function value
const f = function(a) {
console.log(a + 2);
};
// Declare g to be a function
function g(a, b) {
return a * b * 3.5;
}
- 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, 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.
- 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).
-
By wrapping a piece of program in a value and defining new vocabulary.
-
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.
var keyword is visible throughout the whole function that they appear in or throughout the global scope, if they are not in a 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 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. -
Bindings declared with
let
andconst
are in fact local to the block that they are declared in. Functions created withvar
keyword are visible throughout the global scope, if they are not in a 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.