Functions - Reading Assignment

  1. How can you create a function in Javascript?
    

function functionName (variables) {
do something with the variables;
return something;
}

  1. What is the difference between var and let in regards to scope?
    

Var has a global scope which is bad for security
Let was introduced in 2015 to prevent the misuse of var and only has a local scope

  1. What is a pure function?
    

a function that does not rely on other functions to compute it’s returned value

  1. How can you create a function in Javascript?
    You create a function with an expression that begins with the key-word function. Then come the parameters that you can put or omit. Last comes the block with all the statements we have made in order for the function to be/not to be executed.

  2. What is the difference between var and let in regards to scope?
    Var can have a global and a local scope.
    Let only has a local scope.
    In functions, arguments (code to be executed) behave as local variables. In reguards to scope, letdeclares a variable limited to the block, statement, or expression on which it is used. var on the other hand defines a variable globally, or locally to an entire function reguardless of block.
    Sources: Mozilla Developers

  3. What is a pure function?
    A pure function will always return the same results without producing side effects.

  1. a function is created with an expression that starts with the keyword function. functions have a set of parameters and body which contains a statement that is to be executed when the function is called.
  2. let is local to the block it is contained in. var is visible throughout the entire function
  3. a pure function has no side effects and doesnt rely on side effects from other code

1. How can you create a function in Javascript?

  • You can create a function in Javascript by creating an expression that begins with the keyword function.
  • Functions have a set of parameters and a body.
  • The function body of a function is wrapped in braces.

2. What is the difference between var and let in regards to scope?

The difference between var and let in regards to scope is that:

  • var defines a variable globally, or locally to a whole function, regardless of scope.
  • let defines a variable, which is limited in scope.

3. What is a pure function?

A pure function is a type of value-producing function, which doesn’t have side effects, and doesn’t rely on side effects from other code.

2 Likes

How can you create a function in Javascript?
One can create a JavaScript function by inserting a value in a bind. The format of a simple functions is

function nameFunction(Argument) {
    code value;
};

What is the difference between var and let in regards to scope?
The difference between let and var is the way they are implemented. For instance, in let is a method to declare variables that are only limited to in local scope in a block. Meanwhile, var enables to declare a variable globally in a block.

What is a pure function?
From

Elonquent JavaScript

a pure function is:
A specific kind of value-producing function that onlly has no side effects but also doesn’t rely on side effects from other tools

In simpler terms, a pure function is deterministic indicating that when given a single input will return a single output.

How can you create a function in Javascript? Step 1. Assign name and declare it as a function using the keyword “function”. Functions consists of a set of parameters and a body. The body 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? var is visible to the entire program (global), whereas let is only visible within the function (local)

What is a pure function? A value-producing functions that has no side effects nor relies on any side effects.

Question: JavaScript or the book we are using is trying to make “bindings” the default terminology. In my limited experience, “variable” seems to be the accepted terminology. Which one are the professional programmers using?

  1. How can you create a function in Javascript?
    There are several ways:
    y = const function(…) { //body }
    y = let function(…) { //body }
    y = function(…) { //body }
    y = x => x*x

  2. What is the difference between var and let in regards to scope?
    var are global variables, let are local variables ie only valid within the function in which they are created

  3. What is a pure function?
    A function that is self-contained, ie doesn’t cause side-effects and does not rely on variables declared outside of it.

  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. A function can have multiple parameters or no parameters at all. A return statement determines the value the function returns.

    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. [Arrow functions are yet another way to create functions.]

  2. What is the difference between var and let in regards to scope?
    Bindings declared with let (and const) are 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. 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. When you are not sure that a pure function is working correctly, you can test it by simply calling it and know that if it works in that context, it will work in any context.

1 Like
  1. To create a function, you must give it a name like so: const calculation = function(){ /code goes here/ }
  2. Var declares global variables that can be referred to anywhere in the code. Let is used to bind a value local to a block of code.
  3. A pure function is a function that has no side effects nor does it rely on code from outside of itself.

How can you create a function in Javascript?

  • function name (x, y, z) {// awesome code goes here}

What is the difference between var and let in regards to scope?

  • let is local to the block it is declared in
  • var is available to the whole function or global if not in a function

What is a pure function?

  • no side effects and always returns the same value based on the same input

How can you create a function in Javascript?
we create a function by typing const nameOfFunction function() { /body/ }; it can have one or more parameters inside of (), or it can be even without any parameters. Next we need to specify a body of the function, where we say to the program what to do when we call the function. the body has to be specified inside of { }.
What is the difference between var and let in regards to scope?
keyword var is a global function a let is a local function
What is a pure function?
it is a function that makes a result without any side effects.

1 - There are 3 ways to create functions in Javascript:
- Using the “function” keyword as a statement
- Using the function keywoard as an expression
- Using “arrow” notation to write a function

2 - “var” is scoped to the nearest function block, while “let” is scoped to the nearest enclosing block. Both are globally scoped if outside any block.

3 - A value returning function with no side effects and no reference to other functions.

How can you create a function in Javascript?

A function does start with the function keyword. bsp… function test(arg1,arg2) {code}

What is the difference between var and let in regards to scope?

The scope of let is just the block in wich the variable is decleraed. Variables declered with var are accessable all ofer the code.

What is a pure function?

A pure function does not product a side effect and does not rely on global binding that may change. When called with the same arguments, it always produces the same value.

1. How can you create a function in Javascript?
A function is created with an expression that starts with the keyword function.

2. What is the difference between var and let in regards to scope?
let (and const) are only valid in the block they are declared in, while var is visible in the whole function where they are declared in or in the whole program, if they are not defined in a funtion.

3. What is a pure function?
A pure function produces values, has no side-effects and is not dependent on side-effects from other code.

  1. function name_of_your_function (parameters) { body of your function }
  2. let : only referable in a specific block
    var : referable in the whole function where it is declared. if it is declared outside of a function, it can be used in the whole program (global variable)
  3. a pure function always returns the same output to a given input and produces no side effects

How can you create a function in Javascript?

Functions are created as follows -
function name(parameters){
Body of the function which contains the processes
}
A function parameter can be empty or can contain various values seperated by ‘,’

What is the difference between var and let in regards to scope?

the scope of a var effects the entire program outside the function itself as var is “hoisted” to the beginning of the document declaration (aka globalscope)

let is limited to just the function it is created in and does not effect the outside of the function (aka blockscope)

What is a pure function?

A pure function has no “side effects” and the output is always the same for a given input.

  1. const function = { // function code here
    }
    or
    function {
    // function code here
    }
  2. let limits scope to the block, statement, or expression on which it is used. var is global to the nearest function or global scope.
  3. A function with no side-effects and doesn’t rely on side effects.

How can you create a function in Javascript?

function adding(a,b){
return a + b}

What is the difference between var and let in regards to scope?
var is global as long as it isn’t in a fuction
let is local if it is in a block or a function

What is a pure function?
it has no side effects and doesn’t rely on side effects from other functions

1.
A function is created using the keyword function and can be used as;

A. An expression:

const f = function(a) { console.log(a + 2); }

B. A declaration or statement:

function greet(name) { console.log("My name is " + name); }

C. Using an arrow =>

h = a => a + 3;

In general, functions may or may not have parameters and typically have a function body, where the statement to be executed is written in curly braces.

Function name(optional parameters) { statement to be executed; }

2.

Functions that use the keywords let and const are local to the block in which they were created. Code that sits outside of the local block cannot see or call it. Comparatively var has global scope, meaning it can be seen and called from outside the block it was stated in.

3.

A pure function is a function which;

  • Given the same input, it will always produce the same outputs
  • Has no side effects or rely on side effects from other code.
2 Likes
  1. A function is created with an expression that starts with the keyword function
  2. Let declare variables that are limited in scope, var are global variables.
  3. A pure function is a function that not only has no side effects but also doesn’t rely on side effects from other code.