Functions - Reading Assignment

  1. You can create a function in JavaScript either by assigning the value of a function to a binder, using the keyword function:
let myFirstFunction = function(){
  return "Oh Yeah!";
}

or by simply declaring a function:

function anotherFunction(){
  return("Here's another function");
}
  1. Bindings declared with let are local to the block that they are declared in, whereas bindings declared with var are visible throughout the entire function they are in, or they are global if declared outside of functions. So every time you call the function using the same arguments, it will return the same result.

  2. A pure function doesn’t rely on side effects from other code, and it has no side effects of it’s own.

1 Like

1.First put the function keyword, then the function-name. After that is the functions parameters.
e.g function function_name(para1,para2,para3…) //code execute
2.let in binding allows you to declare variables inside a function and it can only be visible locally. However, var can be use globally, which means it can be use throughout the entire program.
3.Pure functions are functions that are not bound to side effects

1 Like
  1. In Javascript 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.

  2. “Let” declares a variable limited to the block, statement or expression on which it is used. “Var” defines variable globally for an entire function regardless of the block.

  3. Pure Function is a value producing function with no side effects and does not rely on side effects of any other function
    ex: function sum(x, y) {return x + y;}

1 Like
  1. A function is programmed by calling on a function and encoding possible/ no parameters and a body. The body consists of statements that are to be executed when the function is called upon. Return statement defines the value a function should return.

  2. "let’ bindings are local and their scope is inside the block of code they are active in. Var variables are global bindings and can be called upon in the whole program.

3.Pure function poduces a value and doesnt produce a side effect whilst simultaneously also not relying on side effects also.

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

It is an expression: first use the keyword function , then optional to define or assign a name
to the funtion followed by a pair of parantheses knowns as parameters. After, we use
curly braces (the body of the function) which contain the statements that the function will
execute when called.
Usually a return statement inside the function determines the value the function returns.
if no expression after the return keyword, it’ll return undefined value to function.

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

Bindings with let and const are local to the block that they are declared in, inside the a loop, the
code before and after the loop cannot access it.
Bindings with a var keyword are visible throughout the whole function pre-2015 that they appear in. or
throughout the global scope , unless they are inside the function.

3.What is a pure function?

a value-producing function that has no side effects and also doen’t rely on side effects from other code
Doesn’t read global bindings whose value might change. when called with the same arguments
it always produces the same value.

1 Like
  1. A function can be created with an expression that starts with the keyword function. It can be declared as any other binding:
const addFunction = function(x, y) {
  return x+y;
};

or shorter version:

function addFunction(x, y){
  return x+y;
};

Another notation uses an arrow instead of the function keyword:

const addFunction = (x, y) => x + y;
  1. Bindings declared with let keyword are local to the block that they are declared in (global scope, function, loop or conditional block etc.) whereas variables created with var keyword recognizes as a block only function (global scope or function).

  2. A pure function has no side effects and doesn’t depend on side effects from other code. When it is called with the same arguments, it always produces the same values.

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

  2. What is the difference between var and let in regards to scope?
    Var is local to the block that they are declared in, if you create one of these inside of a loop, the code before and after the loop cannot “see”

Created with var keyword, are “visible” throughout the whole function

    1. What is a pure function?

A pure function does not depend on and does not modify the states of variable out of its scope.

1 Like
  1. By its keyword function, its parameters between parenthesis and the statements to be executed wrapped in curly brackets. I.e.
const waiterTip = function(billTotal, tipPercentage) { 
   return (billTotal / 100) * tipPercentage;
} 

It can also be declared using the keyword function at the beginning and then defining the binding name. i.e.

function waiterTip(billTotal, tipPercentage) {
   return (billTotal / 100) * tipPercentage;
}

There’s also the arrow notation were the function keyword is replaced for an arrow =>, here first the bind is defined, then the list of parameters wrapped in parenthesis, unless there’s only one parameter, in which case the parenthesis can be omitted, followed by the arrow (=>) and the declaration, the curly brackets can also be omitted if theres only one expression in the body. i,e.

const waiterTip = (billTotal, tipPercentage) => (billTotal / 100) * tipPercentage;
  1. Let is local to the block that is declared in and var is visible to the whole scope.
  2. Is a function that doesn’t rely on other code and has no side effects. It always produce the same value when called with the same arguments.
1 Like
  1. Function always starts with the expression “Function” and it can have parameters in the {} brackets.

  2. Operator “let” is a local binding and it can only be “seen” and work within that function. Operator “var” gives variables a chance to be recognized throughout the all code.

  3. A pure function is a specific function which produces value. It has no side effects and it also doesn’t rely on side effects from other code. When called with the same arguments, it will always return the same values.

1 Like

1.You can declare a function using the function keyword by declaring a function as an expression(value) stored in a variable.
let/var/const/ varaible_name= function(parameters){ definition};
or declare a function as a statement.
function function_name(parameters){definition}

2.A ‘let variable’ declared within a block of code (anything within{}) is local to its particular block. The variable is visible and accessible only inside this block and any nested blocks and functions inside it. a ‘var variable’ work the same way within a function but is not limited to any other blocks of code such as if statement and loops. Any ‘var variable’ declared inside code blocks is visible to the whole program and is considered a global variable. If you have the same global var variable declared inside a block will only result in reassigning the value of such variable instead of a separate entity.
3.A pure function is deterministic(“the same input always produce the same output”) and produces no side effect. It does not depend on or causes change to any external state outside its scope. A pure function’s output is dependent on its input (must use its parameters as input)and must return a value to qualify as a pure function.

1 Like
  1. In three ways: by assigning a binding to a function, declaring a function with the function keyword, or with the => syntax, which is the most concise.
  2. var is scoped to the immediate function but and let is scoped to the specific block it’s in.
  3. A pure function is a function that does not produce side effects and does not rely on side effects from elsewhere in the code.
1 Like
  1. Function in Java Script can be created with using keyword ‘function’ and giving the function a name, followed by the arguments which are going to be used and body of the function, which determines the value of function return.

  2. ‘let’ allows to declare variables limited to the scope of a block; while ‘var’ establishes the global variables.

  3. A pure function - it will always return the same output, with the same input; it also doesn’t produces any side effects.

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

It’s created with an expression that starts with the keyword ‘function’

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

Let is local to the block that it is in and cannot be seen outside of the function, where as var is global and can be seen throughout the entire function

  1. What is a pure function?

When a same input is passed every time, the function will return the same output

1 Like

How can you create a function in Javascript?

A function is created with a keyword expression called function. It is then given parameters to work with, these are in parenthesis. Then, it contains a statement or statements which are to be executed when finally, the function is called. The function, not the keyword function, must always be wrapped in braces.

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

“var” is a global decleration for functions and parameters, while “let” and “const” are localized decleration of a function and thier parameters. The values of a funciton can remain invisible with “let” and “const” while being globally accessable when using var. These local and global environments are called “Scopes”.

What is a pure function?

A pure function has no side effects. It will always produce the same outputs.

1 Like
  1. How can you create a function in Javascript?
    A function is created by using the keyword function followed by the name of the function you want to create.

  2. What is the difference between var and let in regards to scope?
    Let is more limited to the block it is being used in while a value set by var can be used in multiple lines of code in different blocks.

  3. What is a pure function?
    a function that does not create any side effects and is only effected by it’s own parameters. It will always return the same result when the function is called.

1 Like

A function is created with an expression that starts with the keyword function, parameters, and a statement or statements that make up the body.

Var can be seen globally for each block whereas let is seen within its own block environment.

A pure function produces a value, has no side effects and doesn’t rely on other side effects. When called with the same arguments, it always produces the same value.

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

By using a regular binding to a function, e.g.:

const square = function (x) {
	return x * x;
};

It starts with the keyword function. Functions have a set of parameters (functions listing no parameters is also possible), in this case only x, and a body which 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?

let bindings will only be visible visible in the function it is defined in. A variable set by var in a function will be visible outside of a function it is defined in.

  • What is a pure function?

A pure function only depends on its own arguments, it won’t try to change variables out of it’s scope, and it doesn’t produce any side effects.

1 Like
  1. multiple ways:
  • function keyword / binding / arguments / body - e.g. function checkInfo (a, b) { body statements }
    or the same function could be like this
  • const checkInfo = function(a, b) { body statements )
    or like this
  • const checkInfo = (a, b) => { body statements )
  1. var in not restricted to local scope in block statements, only in functions. While let is restricted to local scope in block statements

  2. a function that creates no side effects and does not rely on the side effects of other sections of the code

1 Like
  1. How can you create a function in Javascript?
    function theName() {
    // statement or function body
    }
    ex:
    function icutWood(){
    console.log(“comeherewood!”);
    }
    then run icutWood

  2. What is the difference between var and let in regards to scope?
    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, globally.

  3. What is a pure function?
    A function is called a “pure function” if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something. The only result of calling a pure function is the return value.

1 Like
  1. How can you create a function in Javascript?
  • Write an expression that starts with keyword ‘function’ followed by brackets that optionally can contain one or more arguments. Followed by the function body which contains 1 more more lines of code and may return a value. The function body is enclosed by braces. Finish with a semicolon.
  1. What is the difference between var and let in regards to scope?
  • Variables declared by the var keyword are scoped to the immediate function body while let variables are scoped to the immediate enclosing block denoted by { }
  1. What is a pure function?
  • A pure function is a function which given the same input, will always return the same output and produces no side effects.
1 Like