Functions - Reading Assignment

  1. How can you create a function in Javascript?

Write an expression that starts with the keyword “function”. The function should have a set of parameters and a body which describes the statements to be executed when it is called.

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

“let” is local to the block it is declared in, “var” is visible to the whole function it is contained in, or global if it is not contained within a function.

  1. What is a pure function?

A pure function doesn’t produce a side effect and doesn’t rely on side effects from other code, so it will always produce the same result if given the same arguments.

1 Like
  1. A function is created with an expression that starts with the keyword function.
  2. Parameters and bindings declared in a given scope are local and not visible from the outside. Bindings declared with var behave differently—they end up in the nearest function scope or the global scope. Bindings declared with Bindings declared with var behave differently—they end up in the nearest function scope or the global scope let are limited to the block, statement, or expression on which it is used.
  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

To create function in java script there are two parts a declaration or key word designating a function with or without parameters enclosed and a action statement(body) within brackets telling the function how to operate.

The difference between a Let and vr in regards to scope is that a let is local or within a block where a var statement is global and is seen throughout the entire program.

What is a pure function. A pure function does not have any side effects. Each unique input will always give the same output.

1 Like

1.) A function is created with an expression that starts with the keyword “function”. (P.39)
Function (parameters){function body};

2.) Let and const are local to the block they’re created in making them only visible within that block, but var is global, making it visible throughout the entirety of the function or throughout the global scope without the function. (P.41)
3.) A pure function never changes. It’s a specific kind of function that produces a value with no side effect or use for side effects from other code. It always produces the same value. (P. 54)

1 Like
  1. How can you create a function in Javascript?
    By using the keyword function and having parameters and a body which contains the statements to be executed.

  2. What is the difference between var and let in regards to scope?
    let are local to their specific block. var is visible throughout the entire function as a global variable.

  3. What is a pure function?
    A pure function is a value-producing function that has no side effects but also doesn’t rely on side effects from other code. When called with the same arguments, it always produces the same value.

1 Like

To create a function in Java Script you need a key word called “function” , a name with parentheses to hold or not hold parameters. And finally within brackets a set of code or instruction that need to be operated when the function is called.

The difference between a var and a let statement is that a var is a global binding and a let is a local binding that can only be seen by the block or function that contains it. A global binding can be seen by the entire program.

Take this example script and run this code to get a very clear understanding of how the java Script engine works in defining holding and executing variables that are global vs. local.

function c() {console.log(myVar)}
function b() {var myVar = 3; console.log(myVar);c()}
function a() {var myVar = 2; console.log(myVar);b();}
var myVar = 1; console.log(myVar);
a(); console.log(myVar)

The Java Script is run using a Global Execution stack synchronously step by step. The Java engine will load values and function names but not function attributes until they are called but for the “var” designation that is outside of the function it will load values for these variables in memory.

So if we go step by step we see that the initial value of Myvar = 1 And this is a global binding meaning it will be seen by the entire program.

No lets see what happen when we do successive "console.log (myVar) to see the value of myVar at each stage the code is being executed. The first three lines of code designate functions c(), b(), and a() but these function are not executed until the function is called. Line four is the global binding of

1.) myVar = 1 ( global binding for myVar set in computer memory not printed but held
in execution stack memory until console.log(myVar ) executed. and
myVar = 1

2.) myVar = 2 ( next the function a() is called and put in the Execution stack with its
own Execution context of variables. var myVar =2 for this function a()
and console.log prints myVar = 2. Next within fucntion a() the function
b() is called and executed

3 myVar = 3 (next step in code going synchronously within the function a() is a call
for function(b). This puts function b() at the top of the execution stack
with its own set of variables and Execution context. When b() is
executed var myVar = 3 and console.log(myVar) will print myVar = 3.
The function b() will then call function c() which will now be put on top of
the execution stack with its own set of variables and execution
context.

4.) myVar = 1 (the function c() is called in the next step moving synchronously the
engine will look for the function a() and uses parameters if available
to execute the operation of the function which states
console.log(myVar) but does not define myVar and thus the engine
does not have any variable loaded for function c() and will then look
globally for value of “myVar” and will find myVar = 1 as global binding.

4.) myVar = 1 The next step within the code console.log(myVar)
and the Java Engine jumps out of the execution of function c() and
back into its global environment where myVar = 1 each time
console.log is executed.

A pure function is one with no side effects. Unique input gives the same output each time.

2 Likes
  1. How can you create a function in Javascript?
  • A function is created with an expression that starts with the keyword function.
  1. What is the difference between var and let in regards to scope?
  • Var declared can be called anywhere in the entire function.
  • Let declared are block scoped. Meaning it can’t be accessed outside of a loop if its declared within a for loop for example.
  1. What is a pure function?
  • A pure function is a function which: 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 no way.
1 Like
var functionValue = function(x){
  return x;
}
  1. 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.
  2. 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)
1 Like

Q). How can you create a function in Javascript? in my opinion a function is a small program within a program. it is a specific snippet of code that can be used many times over in a program, A Global Function. ( Saves re inventing the wheel).
A function is created with an expression that starts with the keyword function.

Example below from STACKOVERFLOW:

//Global function existing to serve everyone
function swearOutLoud(swearWord) {
alert("You "+ swearWord);
}
//global functions’ territory ends here

//here is mr. spongebob. He is very passionate about his objects; but he’s a bit rude.
var spongeBob = {
name : “squarePants”,
swear : function(swearWord) {
name = “spongy”;
alert("You "+ swearWord);
return this;
}
}

//finally spongebob learns good manners too. EVOLUTION!
spongeBob.apologize = function() {
alert("Hey " + this.name + “, I’m sorry man!”);
return this;
}

//Ask spongebob to swear and then apologize in one go (CASCADING EFFECT!!)
alert(spongeBob.swear(“twit”).apologize());

Q). What is the difference between var and let in regards to scope?
a). A variable is the name of the storage for a value assigned to identify its location. In JavaScript a variable can be declared using let or var. ‘var’ declaration scope is global irrespective of its declared scope or location whereas the variable declared using let statement is block scoped or enclosed scope.

Q). What is a pure function?
A). A pure function is a function where the return value is only determined by its input values, without observable side effects.

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

    By using the Keyword Function + Name of the function + parameters (a function can
    have no or multiple parameters), and a function body in braces.
    The body contains the statements that has to be executed when the function is called.

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

    let bindings are local to their block (the scope is inside the function), whereas
    var bindings are global, means the bindings created with var are visible throughout
    the whole function or global scope.

  3. What is a pure function?

    A pure function is a value-producing function that has no side effects and doesn’t rely on side effects from other code.
    A pure function doesn’t read global bindings whose value might be changed and it always produce the same value if called with the same argument.

1 Like
  1. One can create a function by declaring it with the keyword “function” with parameters that will perform the code held within the body when evoked.

  2. The difference between var and let in regards to scope is that var is viewable throughout the environment (global), while let is local to the block and is only viewable in that context.

  3. A Pure function is a function that does not cause, nor relies on side-effects to return its value.

1 Like
  • How can you create a function in Javascript?
    Ans: In Javascript, you can create a function by combining the following expressions: ‘function’, parameters/arguments, curly brackets, the body which contains statements which will be executed once the function is called.

           Eg: const abc = function () { }
           Eg: function abc() {}
    
  • What is the difference between var and let in regards to scope?
    Ans: One of them has local scope and the other has global scope. ‘let’ has a global scope while ‘var’ has a local scope.

  • What is a pure function?
    Ans: A pure function not only products any side effects but also doesn’t rely on other side effects to do it’s job.

1 Like
  1. A function is created with an expression that starts with the keyword function.
    function name() {
    // function body
    }
  2. 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. A pure function is a function that doesn’t have any side-effects and doesn’t rely on side-effects from other code.
1 Like

Hi @Day, I guess there was some confusion in the understanding. The above statement is incorrect. In fact, "var" is the one that is functionally or globally scoped (based on instantiation) and "let" is block scoped (only resides in between the brackets).

Hope this clears it out. If any questions please feel free to reach out.

Happy Learning! :smiley:

2 Likes
  1. How can you create a function in Javascript?

There are 3 ways to define a function, which can be called with 0 or more arguments

//Define f to hold a function value
const f = function(a) { return a*a; }

//Declare g to be a function
function g(a, b) { return a*b; }

//Use arrow notation to be less verbose
let h = a => a % 3;

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

Bindings created using the var keyword are visible throughout the whole function they appear in, or throughout the global scope if they are not in a function.
Bindings created using the let keyword are not visible outside the local function they were created in.

  1. What is a pure function?

A pure function is a specific kind of value-producing function which has no side effects, nor does it rely on side effects from other code. When a pure function is called with the same arguments it will always produce the same result.

1 Like
  1. A function is created with an expression that starts with the keyword function
  2. Bindings declared with let are local to the BLOCK that they are declared in only. Those created sith var are visible throughout the whole function that they appear in (throughout the whole scope)
  3. A pure function is a specific kind of value-producing function that has NO side effects and also does not really on any side effects from other code
1 Like

1.you can create a function as a declaration by typing the keyword function then the name of the function followed by parentheses for the parameter the curly braces to place the body of the function.
Like this:
function sayHello(){
console.log(“hello world”);
}
sayHello();
A. You can also create a function expression by storing them in a variable by typing for example :

const greetings = function(){
console.log(“good day!”);
};
greetings ();

B. Arrow function are a shorter modern way to create a function.

let browser = () => ‘hello, world’;
let result = browser();
console.log(result);

C. Callback function can be use as a argument for a function parameter.

let user = [“jimmy”,“anna”,“paul”,“frank”,“billy”];

user.forEach((listUsers,index) =>{
console.log(index,listUsers);
});

D. methods are also functions.
user.forEach

  1. The difference between var and let is that var ignore block scope. let are given
    block level scope.

  2. pure function doesn’t have any side effects. it output the same input every time you call it and it doesn’t affect anything outside of it.

let letter = (s) => {
let r = 33;
r = r + s;
return r;
};

let s = letter(2);
console.log(s);

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.
2 What is the difference between var and let in regards to scope?
let is local to the block it was declared in and var is visible throughout the whole function or throughout the global function
3 What is a pure function?
a pure function is a specific kind of value-producing function that always produces the same value.

1 Like
  1. A function is created in JavaScript with an expression that begins with the keyword “function,” having a set of parameters and a “body” containing the statements that are to be executed when the function is used. The body of the function is wrapped in braces.

  2. “Var” defines variables globally, while “let” defines them locally.

  3. A “pure” function is a function that doesn’t do anything else except for producing a value from given arguments.

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

  2. let keywords are local to the block that they are declared in and cannot be used outside the block, where as var keywords are visible throughout the global scope if they are not in a function.

  3. A pure function is a value producing function that has no side effects; it doesn’t rely on side effects from other codes.

1 Like