Functions - Reading Assignment

  1. A function is created with an expression that starts with the keyword function.
  2. Bindings declared with let and const are in fact local to the block that they
    are declared in.
    Functions using 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 function where the return value is only determined by its input values, without observable side effects.
1 Like
  1. Begin with a function keyword, then name, then parentheses. Parameters may be used or not.
  2. Each var has a scope (local or global). Let is local to the block in which they are declared.
  3. Pure function has no side effects and does not rely on others’ side effects.
1 Like

Functions

Javascript Programming

1.How can you create a function in Javascript?

To create a function we can use a function declaration.

It looks like this:

function showMessage() {

alert( ‘Hello everyone!’ );}

The function keyword goes first, then goes the name of the function, then a list of parameters between the parentheses and finally the code of the function, also named “the function body”, between curly braces.

function name(parameters) {

…body…}

Our new function can be called by its name: showMessage().

For instance:

function showMessage() {

alert( ‘Hello everyone!’ );}

showMessage();

The call showMessage() executes the code of the function.

If we ever need to change the message or the way it is shown, it’s enough to modify the code in one place: the function which outputs it.

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

Scope essentially means where these variables are available for use. var declarations are globally scoped or function/locally scoped.

The scope is global when a var variable is declared outside a function. This means that any variable that is declared with var outside a function block is available for use in the whole window.

var is function scoped when it is declared within a function. This means that it is available and can be accessed only within that function.

var variables can be re-declared and updated

This means that we can do this within the same scope and won’t get an error.

Hoisting of var

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. So var variables are hoisted to the top of their scope and initialized with a value of undefined.

Problem with var

There’s a weakness that comes with var . If you have defined var in other parts of your code, you might be surprised at the output you might get. This will likely cause a lot of bugs in your code. This is why let and const are necessary. While this is not a problem if you knowingly want a variable to be redefined, it becomes a problem when you do not realize that a variable has already been defined before.

Let

let is now preferred for variable declaration. It comes as an improvement to var declarations. It also solves the problem with var that we just covered. Let’s consider why this is so.

let is block scoped

A block is a chunk of code bounded by {}. A block lives in curly braces. Anything within curly braces is a block.

So a variable declared in a block with let is only available for use within that block.

let can be updated but not re-declared.

Just like var , a variable declared with let can be updated within its scope. Unlike var , a let variable cannot be re-declared within its scope. However, if the same variable is defined in different scopes, there will be no error:

This fact makes let a better choice than var . When using let , you don’t have to bother if you have used a name for a variable before as a variable exists only within its scope.

Also, since a variable cannot be declared more than once within a scope, then the problem discussed earlier that occurs with var does not happen.

Hoisting of let

Just like var , let declarations are hoisted to the top. Unlike var which is initialized as undefined, the let keyword is not initialized. So if you try to use a let variable before declaration, you’ll get a Reference Error.

3.What is a pure function?

The definition of a pure function is:

The function always returns the same result if the same arguments are passed in. It does not depend on any state, or data, change during a program’s execution. It must only depend on its input arguments.

The function does not produce any observable side effects such as network requests, input and output devices, or data mutation, console.log, HTTP calls (AJAX/fetch), changing the filesystem (fs), querying the DOM

1 Like
  1. A function is created with an expression in Javascript is defined with the Keyword Function followed by brackets containing 0 or more parameters, followed by the function body in Curly braces:

  2. Binding delcared with let are local to the block that they are declared in. Functions created with Var are visable through the whole function that they appear in or throughout the global scope.

  3. A pure function has no side effects and doesnt rely on side effects either. It always produces the same value when called with the same arguments. You can test it by calling it and know if that works in that context, it will work in any context.

1 Like
  1. There are 3 ways.
    1. define a binding to hold a function: e.g. f = function (x,y) {…};
    2. declare a binding to be a function. e.g. function g (x,y) {…};
    3. arrow function: var h = (x,y) => {…};
2. the cope of let is only to the function, while var's scope is global. 3. A pure function does not produce side effects, nor uses any side effect.
1 Like

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 is local and not visible from the outside a function and Var can be seen globally (i.e. throughout the program.

3. What is a pure function?
A pure function has the… property that, when called with the same arguments, it always produces the same value (and doesn’t do anything else).

1 Like
  1. With an expression that starts with keyword function, it isn`t necessary for functions to have parameters but they usually have. Parameters are written in () and statement or body are written in {}.
  2. Let is defining variable that is limited to the block, statement or expression in which it is used. Var is declaring variable that can be global but also local scope.
  3. Pure function is value producing function that has no side effects and it is not relying on global bindings that may change. When it is called with same parameters it always produces the same value
1 Like
  1. How can you creat a function in Javascript? (A.) 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.

  2. What is the difference between var and let in regards to scope? (A.) 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.
    It can be said that a variable declared with var is defined throughout the program as compared to let.

  3. What is a pure function? (A.) Given the same input, will always return the same output. Produces no side effects.
    A dead giveaway that a function is impure is if it makes sense to call it without using its return value. For pure functions, that’s a noop.

1 Like

How can you create a function in JavaScript:
Starting with the key word function with the name followed by parentheses containing an argument then a body within curly brackets.
What is the difference between var and let in regards to scope:
When you declare a variable with var it’s scope is not limited to the block in which it’s defined it’s limit is within the function, whereas let is limited to the block.
What is a pure function:
A pure function given the same input will always produce the same output with no side effects.

1 Like
  1. You can create a function in Javascript by first declaring that you want to perform a function by writing “function”. Then you give it a name you can refer to. The name is followed by smooth brackets which contain characteristics called “parameters”. After the smooth brackets come curly brackets which contain the code to be executed.

  2. The difference between “var” and “let” in regards to scope is that “let” shows a variable which is limited to the immediate block of code it is in, while “var” can be used anywhere in the function.

  3. A pure function is a function which will always return the same output if given the same input, and will not produce side-effects.

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

It is created like this : first - an expression that starts with the keyword function. Then we add one, more then one ore none parameters and a body (with statement).

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

Var declares a variable to entire function and let declares a variable limited to the block.

  1. What is a pure function?

It is a specific function that has no side effects and oesn’t rely on side effects from other code. When called, pure function always produces the same value.

1 Like
  1. To Create a function in Javascript a new binding needs to be created with function keyword.
    const makeNoise = function() {
    console.log(“Pling!”);
    };

  2. Let is local, seen only inside the block they are created in.
    Var bindings can be seen globally across the program.

  3. A pure function is a value producing function with no side effects, it doesnt read global bindings which value might change.

1 Like
  1. To create a function you must define it by writing function, naming the function, and then writing what the function will do.

function myFunction(x){x+1};

  1. var has a function wide scope whereas let has a block based scope. This means that var must be referenced within the function in which it was declared. let however, must be referenced within the same { } bracket block as that let variable declaration.

  2. A pure function always produces the same output for a given input. Pure functions also never produce side effects. This way, pure functions can serve as very plug-and-play solutions to reoccurring problems.

1 Like
  1. function functionName(parameters) {code to execute}
  2. let is only visible within the function that it is created in and var can be called from outside the function.
  3. A pure function is a function that does not create any side effects or rely on side effects.
1 Like
  1. An expression that starts with the keyword function

  2. let are local to the block they are declared in and var are visible to the whole function

  3. a value producing function that has no side effects and doesn’t rely on side effects from other code

1 Like
  1. Function name = Function(x) {
    return x * x;
    };
    console.log(square(12));

  2. Var can be a global variable and let is a local variable.

  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

1 Like
  1. Function code:
    const square = function(x) {
    return x * x;
    };
    console.log(square(12));
1 Like
  1. To create a function you have to start with the keyword function then the parameters and the body which contains the statements to be executed when the function is called.
  2. Var is globe in that the whole function is visible throughout the whole function
    let is just local in its loop function and the coding before and after cannot see it
  3. Pure function is one that has no side effects and doesn’t rely on side effects from othere coding. It doesn’t read global and when presented with the same argument it will give the same answer
1 Like
  1. You can create a function by binding its name, defining its parameters, and then writing the statements that the function follows. This has a few different notations:

Function as a value;
let greetFamily = function(){
console.log(hello!);
}

With function keyword;
function cleanHands(numSec){
if (numSec >= 14){
return true;
}else {
return false;
}

  1. The let keyword assigns a local scope, while var takes either global scope, or the scope of the closest function.

  2. A pure function is special value type function that both does not produce side effects, but also does not rely on values from any other side effects. This has the special property such that when the function is called with the same arguments, it always produces the same result.

1 Like

Hector A. Martinez, Assignment-Functions 08/30/2020

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 statement that are to be executed when the function is called.

2.What is the difference between var and let in regards to scope?
let are local bindings to a function block, var are visible throughout the global scope.

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

1 Like