Functions - Reading Assignment

1. How can you create a function in Javascript?
By using a keyword

function name() {
 //our code

}

then call the function name()

2. What is the difference between var and let in regards to scope?
var is accesible from outside the function in global scope if is not in function, let just from inside declared block.

3. What is a pure function?
Any function that gives us the value - without any side effects.

1 Like
  • How can you create a function in Javascript?
    You can create a function with an expression. Keyword = function. Then followed by parameters and the body of the functions.

  • What is the difference between var and let in regards to scope?
    Let shows a local point within the blocks & Var is accessible throughout the program.

  • What is a pure function?
    A value producing function without side effects.

1 Like
  1. A JavaScript function is defined with the “function” keyword, then followed by a name, followed by a parentheses. The functions names can contain letter and numbers for example, and the parentheses may contain parameter names. The code to be executed by the function is placed in-between curly brackets “{}”.

  2. Main difference is scoping rules. Variables declared by “var” keyword are scoped to the immediate function body (hence the function scope) while “let” variables are scoped to the immediate enclosing block denoted by “{ }” (hence the block scope).

  3. A pure function is a function that has the following properties: 1. Its return value is the same for the same arguments. 2. Its evaluation has no side effects.

1 Like

How can you create a function in Javascript?

There are three ways in which this can be done.

1. A regular binding whose value is a function. This is referred to as ‘defining a function.’

const add = function(a,b) {

return a + b;

};

2. The second method is called ‘declaring a function.’ It is a slightly condensed version of the first example. The ‘function’ keyword is used at the beginning of the sentence.

function subtract(a,b) {

return a - b;

}

3. The last option is called the ‘arrows function.’ It’s the shortest version.

const multiply = (a,b) => {return a*b}

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

Both keywords refer to variables, or bindings. Variables declared with var are function-scoped, meaning their scope is restricted to the function they were declared in (variables declared with var outside of a function are globally-scoped). Variables declared with let are block-scoped, meaning their scope is restricted to the block they were created in (variables declared with let outside of a function are globally-scoped).

What is a pure function?

  • A pure function is a function that produces no side effects.
  • It is its own little independent bubble, easy to move around if re-organizing is needed and immune to the side effects of other code.
  • It is also deterministic, i.e “when called with the same arguments, it always produces the same value” - Eloquent Javascript.
1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

  1. Create a function by input function (parameters,parameters,…) {task to be performed}

  2. “var” creates a binding of a variable that can be used within its scope such as within the function,block or whole program, depending on where this “var” is written in the code. “let” always creates a binding local to its block.

  3. A pure function is a function which produces the same output with its respectively input, without side effects. It can be used throughout the program as tool by inputting parameters into the function. It does not affect or become influenced by external states.

1 Like

1: By using the keyword; function. Functions have a set of parameters and a body that contains the statements that will be executed when the function is called.

2: let is local to the block its declared in, so if you create it inside a loop the code surrounding it won’t be able to see it. var is global and thus visible to the entire function. If var isn’t in a function it is visible throughout the global scope.

3: A pure function is a “node-side-effect-” function whose value won’t change, as it doesn’t rely on side effects from other codes. It doesn’t read global bindings whose value might change. This means, when called with the same argument, it will always answer with the same value.

1 Like

1. How can you create a function in Javascript?
A function starts with the keyword function, then the parameter in parantheses and after the curly brace
we have the body.

2. What is the difference between var and let in regards to scope?
Var is visible for the whole program and let is only visible local to the block that it is declared to.

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

1 Like

Javascript functions are created by using the keyword function followed by a descriptive function name and opening and closing parentheses ( ), followed by lines of code wrapped by curly braces., like so:

function (parameter1, parameter2) {
  //this function does nothing
  //lines of code would go in here
  //this block is called the body of the function
}

Values can be passed to a function when parameters are specified inside the function parentheses when the function is called. Javascript is flexible in the way it handles too many or too few arguments passed when a function is called. If too many arguments are passed, the extra ones are ignored. Missing arguments are assigned as undefined.

Bindings work within a scope or area of a program. Variables declared outside of a function or block have a global scope, meaning they are recognized throughout the whole program. Variables declared inside a function or block are only recognized in the area they were declared in and can’t be seen globally, so they are termed local bindings. The keywords var and let are both used to define bindings but the scope of var is broader than let. If let is used to define a variable in a block that’s inside a function, that variable will only be recognized inside the block and not in the whole function. If the same variable was instead defined using var, then it would be seen by the whole function and not just inside the block.

A pure function is like a math function that produces the same output every time it’s called with a given set of arguments. A (pure) math function would be the addition of two numbers. Every time the same numbers are added together the result is the same and that’s all there is to it. Pure functions don’t produce side effects. A side effect is like printing information to a screen or some other user input/output. A pure function cannot rely on side effects of other functions for input, like using the current time or generating random numbers as both would change each time the function would be run. Random values could be passed to a pure function, but couldn’t be generated inside a pure function.

1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

1 Like
  1. How can you create a function in Javascript?
    A function starts with the keyword function followed by different parameters, written in braces. After that comes the body, which contains, how the function should execute.

  2. What is the difference between var and let in regards to scope?
    Let (and const) only act local, which means they work only inside the block, they are defined in. Otherwise var works globaly.

  3. What is a pure function?
    A pure function is a specific kind of value-producing function, that has no side effects and doesn’t rely on other side effects from other code.

1 Like

Nice answers @Speed_Lizard :ok_hand:

Just a couple of additional comments about Q2…

  • Variables declared with let are also available globally if they are declared outside of all code blocks and functions.
  • Variables declared with var within a function are only available within that same function.

Take a look at this post for some clarification. And if you would like to go a bit deeper, then try this post.

2 Likes

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

2. 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. Whereas var bindings are visible throughout the whole function that they appear in.

3. What is a pure function?
A pure function is a specific value-producing function thas has no side effects and doesn’t rely on side effects from other code.

1 Like
  1. How can you create a function in Javascript?

A function is created with an expression that starts with the keyword “function” and they can have a set of parameters (not mandatory) and a body, which contains the statements that are to be executed when the function is called.

function name( parameter ){ body };

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

var is a global binding, these are defined outside of any function or block and their scope is the whole program, these can be referred to wherever you want.
let is a local binding, which is declared inside a function and can be referenced only in that function

  1. 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.

1 Like
  1. How can you create a function in Javascript?
    A function is created with and expression:

function xyz(Parameter) {Code}

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

var: firstVariable = “I’m first!” // Declared and initialized
let: secondVariable; // Simply declared.

  1. What is a pure function?

Its return value is the same for the same arguments. 2. Its evaluation has no side effects. 3. computational analogue of a mathematical function.

1 Like

1.) Create a function with the following binding
var x = function(arg_1, arg_2,…,arg_n){blah blah do stuff return value}
2.) Let declares local block level variable while var declaration is global - function scope.
3.) Pure is a one to one function. Meaning for every input there’s a unique output. The same input would also have to generate the same output without any side effects.

1 Like
  1. How can you create a function in Javascript?

A: A function is defined with the function keyword, followed by a name, followed by parentheses()

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

A: You cannot use them inside of loops as they are defined or local to the block.

  1. What is a pure function?

A: Are functions that accept an input and return a value without altering any outside data, known as (side affects).

1 Like
  1. How can you create a function in Javascript?
    You can create a function by assigning the keyword ‘function’ a binding, where the value of the binding is a small block of code that will be performed when the function is called. The function can also include parameters.
    For example:
function cleanUnderwear(x){
  if (x>5) {
    return "Plenty of underwear, don't stress";
  } else if (x > 1){
    return "You should wash your underwear more often";
  } else if (x == 1){
    return "You should probably wash some underwear today";
  } else if (x == 0){
    return "Oh Oh, Commando? or Inside Out?  Hmmm......";
  }
}
console.log(cleanUnderwear(4));

// Result will be - "You should wash your underwear more often"
  1. What is the difference between var and let in regards to scope?
    The difference between ‘var’ and ‘let’ is that ‘let’ is only visible within the block it is created in (including subsequent blocks that are nested within that block) and ‘var’ is visible to the whole function in which it is created. Both can look inwards but not outwards from the function, for example if either is defined globally, then they can be used within functions (and their nested blocks), while if they are defined within a function, they can only be used locally in that function and not globally.

  2. What is a pure function?
    A pure function is a function that produces a value, does not produce side effects nor does it rely on side effects from other parts of code (for example global bindings that are subject to change). A pure function depends on its own arguments and may also rely on other specific values, defined within the function, that are not subject to change.

1 Like
  1. By typing keyword function name ( parameter1, parameter2, parameter3) {
    // code to be executed
    }
  2. Variable let have their scope in the block and in any sub-blocks with main difference the
    scope of var variable is the entire closing function.
  3. A function is pure if it’s free from side-effects and returns same output, given the same input.
1 Like

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

Not quite right sir(s), the var variables are used for global scope (meaning all functions on the program have access to this variable). While the let variable is permissioned to the block that exist (like a variable that is going to be use only for a function’s logic, so functions from the outside of it’s existing function would not have access to it).

Hope this gives you a clear view of the subject, keep learning! :

If you have any doubt, please let us know so we can help you!

Carlos Z.

1 Like