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 (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.
An expression:
const square = function(x) {
return x * x;
};
A declaration or statement
function square (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 in, the code before and after the loop cannot âseeâ it. 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 has no side effects, also doesnât rely on side effects from other code.
When called with the same arguments, it always produces the same value.
-
How can you create a function in Javascript?
The keyword function followed by a set of parameters. Function(x,y){returnâŚ}. -
What is the difference between var and let in regards to scope?
Let and const are local bindings and only usable in the block they are declared in. Var is global binding and usable all over the program. -
What is a pure function?
Its a function that always produce the same value from the same argument, and has no side effects.
-
How can you create a function in Javascript?
- In order to create a function in Javascript you would have to use the function key they are three methods. Known as a âfuntion declarationâ you can just declare the function by using the âfunctionâ keyword followed by the name defining it and then followed parentheses for the aguements and brackets for the body. Another method is to declare the function assigned to a value as you would when declaring variables. The third way is to declare the fuction assigned to a value without the âfunctionâ keyword and also using the â=>â before the code for the body.
-
What is the difference between var and let in regards to scope?
- Variables, when declare with let with a local enviroment will not be seen in the global enviroment. Code writing outside in the global scope wouldnât be able to ferrence and use that variable. Had the variable been declare with var then code in the global enviroment wouldnât be able to see it.
-
What is a pure function?
- A pure function is a function that has no side effects nor does it rely on side effects from aspects from any of the other 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.
A function is created with an expression that starts with the keyword. 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?
var
is a definition which is function scoped but let
is block scoped. So what is that Scope thing?
Besically, let
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var
. var
is rather a keyword which defines a variable globally regardless of block scope.
Besides like mentioned in Eloquent Javascript book, â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.â, so then we can still see some var
way of examples around.
Good Examples to understand: Difference between let and var in Javascript
More about let
and variable scopes
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.
Hi @MasterofCoin,
Excellent answer.
However, In the below statement I believe you meant when the variable is declared with var
, they WOULD be able to access in the global environment.
Hi @Atlas,
That is an excellent and thorough answer from your side. Appreciate your efforts into learning the intricate details of this language. Cheers !
Happy Learning!
1. How can you create a function in JavaScript?
A function is defined with the keyword function, followed with a name (that the function binds to), followed with parentheses () that may or may not contain parameters*, followed by braces {} that contain the code to be executed by that function.
function myFunction(parameter) { code block }
(* The parameters of a function behave like regular bindings, but their initial values are given by the caller of the function, and not the code inside the function body.
2. What is the difference between var and let in regards to scope?
The keyword let declares a binding that is limited to the code block in which it is used, while the keyword var declares a binding globally, and can be seen by functions regardless of code block. If for example a code block that is nested within another code block declares two bindings, one with var and one with let. The binding declared with let will not be visible outside of this code block. While the binding created with var is visible globally and can be used by other functions.
3. What is a pure function?
A pure function is a deterministic function - that when given the same input, always produces the same output/result. They donât have any side effects, so they donât log anything to the console or alter any external state or mutate any original values in a program. They mustnât depend on anything outside of itself, whether it be time of day or the state of a network, disk, or user input - but rather, be completely independent as well as predictable. These functions really useful and can be moved about and reused inside a program as many times as you want.
- A function is created with an expression that starts with the word function. Functions may or may not have set parameters
- Let allows you to declare variables. which are limited to the scope of that block. Var keyword defines a variable globally, or locally to an entire function regardless of block scope.
- A pure function is a function where the return value is only determined by its input values, without observable side effects.
1: by using the keyword function and defining a functionâs parameters(can be empty) and body.
2: let is used locally
Var is used globally
3: a deterministic function . This means when the same input is passed every time, the function will return the same output
Excellent answers sir, well documented! Please keep them like that
Carlos Z.
- To create a function in JavaScript, you have three options:
- Function definition: const exampleFunction = function() {}
- Function declaration: function exampleFunction() {}
- Arrow function: const exampleFunction() => {}
- With var, the scope is global (or local to the function regardless of block scope), but with let, the scope limited to the local block of the function.
- A pure function is a function that given the same input will always return the same output and it produces no side effects.
-
How can you create a function in Javascript?
Using the keyword function, with a body wrapped in braces, and expression is created. -
What is the difference between var and let in regards to scope?
Let is only visible within the local function, while var is âglobalâ in scope even when placed inside a function. -
What is a pure function?
A function that has no side effects and doesnât rely on other codeâs side effects.
Hi @odesseus011 excellent answers, little clarification on var. it is only global inside a function if it is declared inside the function.
- create an expression that starts with âfunctionâ, define parameters between () and then the statements that need to be execute between {}.
- var will are visible in a whole function or globally, let only in the block they are created in.
- 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?
Letâs do an exercise from the book and make a function called min that returns smallest of the arguments we input.
- We can use the function keyword:
- Here is a slightly shorter way:
- We can also make an arrow function:
Basically, we state that it will be a function, we give a name to that function, provide arguments in ( ) and define in { } what the function will do.
- What is the difference between var and let in regards to scope?
- let bindings only work in local scope. That is, if we create a let binding between { }, it wonât work outside.
- var bindings work globally. Exception is if they are in a function. A function creates a new block and var inside it wonât work outside.
- What is a pure function?
- A function that doesnât produce any side effects and also doesnât rely on side effects created by other functions.
- Pure functions donât read global bindings that may change value.
- Pure functions will always return the same value from the same arguments. They are not affected by any context. If it works now, it will work later.
I just watched Ivanâs vid on local and global variables. I think I get it now.!!
Thanks for the feedback.
Thank you and yes I did mistype regarding var and itâs ability to access the global enviroment. Thanks for pointing that out. I will have to take more care to proof read my answers.
-
How can you create a function in Javascript?
You can create a function by using expression that starts with the keyword function. -
What is the difference between var and let in regards to scope?
Oneâs (var) scope is global and another oneâs (let) scope is local, the block where itâs defined. -
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. 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.
- How can you create a function in Javascript?
I can define one with a binding for it not to be changed. I can also declare one by beginning an expression with the keyword function. A function declaration isnât part of the top-to-bottom flow control - this makes it possible for its expressions to be left undefined, producing a side effect.
- What is the difference between var and let in regards to scope?
var relates to the entire function while let will be considered only in the block in which it is declared.
- What is a pure function?
One that produces no side effects and when given the same input will always produce the same output.
1: function //functionname(//parameters given for the function)
{//code to be executed}
2: A let declaration is confined to the statement/expression it is used in
a var declaration is global to a program (or function)
3: A pure function doesnât depend on and doesnât modify the states of variables out of its scope