Functions - Reading Assignment

How can you create a function in Javascript?

  • A function is defined with a keyword function , followed by the name and then the phrases parenthesis.

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

  • let is used in the local scopes while var is used in the global scope

What is a pure function?

  • a pure function is a function that produces the same value every time you call it, with no side effects.
1 Like
  1. It is created by the keyword function followed by the parameters in quotes, followed by the statement to be executed which is enclosed in brackets.
  2. let declares a value to a variable limited to the scope of the block, statement, or expression in which it originates. var declares a value that is visible throughout the whole function that they appear in—or throughout the global scope if they are not in a function.
  3. A pure function always produces the same results given the same parameters without side effects
1 Like
  1. You can create a F using keyword “function” followed by () after it’s name.
  2. Var is seen in the global scope.
  3. Pure function has no side effects and doesn’t rely on side effects from other code.A pure function when called with the same arguments always produces the same value (and doesn’t do anything else).
1 Like
  1. How can you create a function in javascript?
    By an expression with the keyword function
  2. What is the difference between var and let in regards to scope?
    Let declares a variable limited to the scope, var creates a variable either globally or locally
  3. What is a pure function?
    A value producing function which doesn`t have any side effect and doesn’t rely on other codes side effect
1 Like
  1. How can you create a function in Javascript?
var myBinding = function() {
    return 'this is a function decleration';
}
  1. What is the difference between var and let in regards to scope?

var declared bindings are visible in the local or global scope (function or environment), let and const declared bindings are visible within the local block

  1. What is a pure function?

Same inputs always return same outputs + no side-effects

1 Like
  1. Function Key word , Function Name , Parentheses ()

let = local ( can not be seen outside of block)
var = global ( can be seen by whole program)

Pure Function given the same input, will always return the same output

  • produces no side effects
1 Like

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 contain the statements to be executed when the function is called. The function body of a function created this way must always b wrapped in braces, even when it consists of only a single statement. A function can have multiple parameters or no parameters at all.

ex:
const function(param1,param2,…){
[BODY - STATEMENTS GO HERE]
};

What is the difference between var and let in regards to scope?
let bindings are local to the block that they are declared in, so if you create a let inside of a loop the code before and after the loo can not “see” it.
Var bindings 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 has a return vau that is the same for the same arguments (no variation with local static variables, non-local variables, mutatable reference arguments, or input streams from I/O devices. Also, a pure function does not have any side effects when evaluated (no change of variables or I/O streams).

1 Like
  1. A JavaScript function is created with an expression that starts with a keyword function.

  2. Let is local to a particular block; Var is the global variable.

  3. A pure function produces a value (that does not change) with no side effect from other code (whose values may change.)

1 Like
  1. 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 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 also be created using the Arrow (=>).

  2. 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. 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. Nonpure functions tend to require more scaffolding to test.

1 Like
  1. A function can be created with 4 key parts, the function keyword, the name of the function, () to define each variable in the function, and {} to house the code to be executed.

  2. In regards to scope, let is local to the block they are declared in, var is global to the entire function, or program.

  3. A pure function is a function that produces value, has no side effects, and does not rely on side effects from other code.

1 Like

1.How can you create a function in Javascript?

You use keyword "function, give it a parameter and the body.

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

var defines a variable globally or locally to entire function regardless to scope. let allows declare variables that are limited to scope.

3.What is a pure function?

It produces value with no side effects.

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

function functionName(){
//do something here
}

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

let is local, var is global

  1. What is a pure function?

A function is pure when it is only acting inside itself and is not changing any global variables or variables in another function, and is not producing side effects such as alerts.

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

Create a binding to a function name, with or without parameters. Add functionality to what will happen when the function is called.

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

var sets a wider scope; let keeps tight locality to the scope in which it is used.

  1. What is a pure function?

A function that does the same thing every time independent of global variables/bindings and with no side effects output.

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

JavaScript function is a block of code designed to perform a particular task.
A JavaScript function is defined with the function keyword, followed by a name,
followed by parentheses ().

function name(parameter1, parameter2, parameter3…) {code to be executed}

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

let and const use block scoping,
they won’t appear locally. Var uses the global scope if they are not declared in a function.

  1. What is a pure function?

A pure function is a function that given the same input will always
return the same output and it producess no side effects.
They are completely independent of outside state, do not alter any external states,
easy to move around and reuse throughout a program, and great for future adaptations.

1 Like
  1. A function can be created the same way any other binding for a value can be created. Rather than binding an input to a value, you are now binding it to a function.
  2. with regards to scope, var can be seen outside of a function it was created in while let can only be seen within the function it was created in.
  3. A pure function is a function that does not produce any side effects, as well as does not rely on side effects from other code.
1 Like
  1. How can you create a function in Javascript?
    To declare with a variable and make it equal to a function.
    Ex. const func = function() { //code inside };
    To write function then the name after it.
    Ex. function func() { //code inside};
  2. What is the difference between var and let in regards to scope?
    let and const use block scoping, they won’t appear locally. Var uses the global scope if they are not declared in a function.
  3. What is a pure function?
    It doesn’t produce or rely on side effects, avoids reading global bindings.
1 Like
  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.

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

Ok, so. It took me a while to understand that, but only because I was also watching The Net Ninja lesson re that subject, cos I like the way he is explaining things, its very clear to me:

https://www.youtube.com/watchv=kjdZ1h3CIs4&list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET&index=22

Unfortunately, this particular lesson seem to be incorrect and it probably got to do with the fact that was recorded in 2015. Am I correct? Why he says var is not global no matter of location and in addition to that he proves that in the console.log, so it is very very confusing.

Anyway according to the book and the other sources and my tests, the difference between var and let in regards to scope is that var is global no matter where is located and let is local within only the function that is located or global if it is located outside any function.

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

to create a function within Javascript an expression starts with a keyword function and set of parameters to be used within the function the body of the function you will create a statement that will run when the function is called.

  1. let is only visible within the function that it is created in and var can be called from outside the function.

  2. A pure function does not create any side effects or depend on side effects

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

Create a binding that refers to a function (use the keyword ‘function’) then set the parameters of the function (i.e., what do you want it to do?) A function can produce values or merely side effects.

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

‘Let’ is local to the block they are declared in, it only acts within that scope, whereas var is global and acts throughout the function, or the program as a whole.

  1. What is a pure function?

A function that returns a value but doesn’t have any side effects. It always produces the same value when summoned with the same arguments. A pure function will not read global bindings as they may change.

1 Like
  1. A function is created with an expression that starts with the keyword function.
  2. let is local to the block that they are declared in while var is visible throughout the whole function that they appear in—or throughout the global scope.
  3. Pure functions are functions that accept an input and returns a value without modifying any data outside its scope.
1 Like