Functions - Reading Assignment

1. You can create a function in Javascript by an expression that starts with a keyword function.

2.
Var - is a functioned that is scoped.

let - let is a function that is block scoped meaning its scoped to the enclosed code. {enclosed code}

3. A Pure Function has no side effects and doesn’t have to rely on other side effects from other code.

1 Like
  1. Functions are a type of binding created by using the keyword “Function” to begin an expression, where the function can have a set of parameters defined, as well as a body which contains all the statements to be executed by the code when the function is used. The body must be contained within braces.

  2. All bindings defined with “let” are local to the block they are defined in, whereas “var” bindings are visible throughout the whole functions that they appear in, and if they don’t appear in a function then they are recognized on the global scope.

  3. A pure function is defined as a value-producing function type that returns no side effects and also does not depend on any side effects from other code (it cannot read global bindings whose value might change because of this, for example). Upon calling a pure function with the same arguments it will always produce the same value.

1 Like

1. How can you create a function in Javascript?

There are four ways a function can be created in JavaScript. They are as follows:

(i) A function as a statement

function Add(num1,num2){
let sum = num1+ num2;
return sum;
}
let res = Add(7,8);
console.log(res); // 15

(ii) A function as an expression

let add = function a(num1,num2){
let sum = num1+ num2;
return sum;
}
let res = add(8,9);
console.log(res);// 17

(iii) A function as an arrow function

var add = (num1, num2)=> num1+num2;
let res = add(5,2);
console.log(res); // 7

(iv) A function created using the Function constructor

var add = Function(‘num1’,‘num2’,‘return num1+num2’);
let res = add (7,8);
console.log(res); // 15

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 while a variable declared with let is defined just to be used inside a function.

3. What is a pure function?

A function must pass two tests to be considered “pure”:
1. Same inputs always return the same outputs
2. No side-effects

1 Like

1 using the keyword ‘’ function ‘’ and write a name after it

2 let contains keywords that can be used just within its own block, var can be extended to the entire function

3 is an indepedent one without 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”. It has a set of parameters and a body which contains the statements that will be executed when the function is called.

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

Bindings with “Let” and “Const” are local to the block they are declared in.
Bindings with “var” are visible locally, if they’re in a function, or globally if not in a function.

  1. What is a pure function?

Specific type of value producing function that not only has no side effects, but also doesn’t rely on side effects to from other code.
When its called with the same arguments, it always produces the same value.

1 Like