Functions - Reading Assignment

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.

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. 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—forexample, 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).

  1. A function is created with an expression that starts with the keyword ‘function’. functions have parameters and a ‘body’. which contains the statements which are to be executed when the function is called. The function body must always be wrapped in braces.

  2. ‘let’ is local or declared inside the block that it is declared in in the function and can be referenced only in that block whereas ‘var’ is global in scope and is visible throughout the whole function that it appears in - or throughout the global scope, if it is not in a function.

  3. A pure function is one that returns a value, has no side effects, and also doesn’t rely on side effects from other code. - for example it doesn’t read global bindings whose value might change. When it is called with the same arguments it always produces the same value.

  • How can you create a function in Javascript?
    a function is created with the keyword function.
  • What is the difference between var and let in regards to scope?
    var is a global binding and let is a local binding
  • What is a pure function?
    A function that doesn’t modify variables outside of it’s scope and returns the same result given the same parameters.
  • How can you create a function in Javascript? Function (n){} //n is the variable in this case
  • What is the difference between var and let in regards to scope? scope is where is the function belong in the POO perspective
  • What is a pure function? Piece of program

How can you create a function in Javascript?
A function start with the keyword ‘function’ then you give it a name and it can take parameters or not, and finally between curly brackets you have the code that the function does. example :
function square (n) {
return n*n;
}
What is the difference between var and let in regards to scope?
‘var’ is the keyword that make the variable global. It s a variable that can be see in the whole program no matter the scope. And let is a variable that is going to be seen only in the scope that it was declared.
What is a pure function?
A pure function is that one that its return doesn t produce side effect , only works with its own parameters and its own environment.

How can you create a function in Javascript?

A function in javascript is a regular binding where the value is determined from the code that follows, parameters are allowed and a value is returned. It starts with the keyword function, followed by optional parameters, body and a return value.

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

Th difference is a scoping difference, var is scoped to the nearest function block, let is scoped to the nearest enclosing block.

What is a pure function?

A pure function, given the same parameters returns the same results all the time with no side effects.

1 Like

How can you create a function in Javascript?
Function is created with an expression that starts with the keyword function

What is the difference between var and let in regards to scope?
Var keyword, are visible throughout the whole function they appear in or throughout the global scope
Let are 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 cannot see it.

What is a pure function?
Is a specific kind of value producing function that not only has no side effect but also doesn’t rely on side effects from other code

1. How can you create a function in Javascript?
A function can be defined as a regular binding where the value of the binding is a function:
const square = function(x) { return x * x; };
It can also be declared like this:
function square(x) { return x * x; }
Functions have a set of parameters and a body which contains the statements to be executed.

2. What is the difference between var and let in regards to scope?
Bindings declared with let and const are local to the block that they are declared in. For example, if you declare a binding with let inside of a loop, the code outside the loop will not be able to access the binding. When a binding is created with the keyword var, however, it is accessible throughout the whole function it appears in.

3. What is a pure function?
A pure function produces a value, has no side effects, and does not rely on side effects from other code. When it is called with the same arguments, it always produces the same value.

2 Likes

1. How can you create a function in Javascript?
Most common function in Javascript which is defining new vocabulary. 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 limited in scope to the block while Var defines variables to a function regardless of block scope.

3. 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. For example const right = function(x) { console.log(x); };
  2. In functions, arguments (code to be executed) behave as local variables. In reguards to scope, let declares a variable limited to the block, statement, or expression on which it is used. var on the other hand defines a variable globally, or locally to an entire function reguardless of block.
  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. How can you create a function in JavaScript?
A function is created using the function keyword followed by the variable name and brackets.

2. What is the difference between var and let in regards to scope?
When we use the keyword “var”, the variable is inside the scoop of the function. Using the keyword “let” means that it will only be seen inside of the block.

3. What is a pure function?
This function has no side effects and if it is passed a parameter it will always return the same value. Meaning, no other functions can change it.

  1. const func = function() {//code}
  2. let and const use block scoping, so they won’t appear locally.
    Var uses the global scope if they’re not declared in a function.
  3. Doesn’t rely on side effects and doesn’t rely on them.

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? Bindings declared with let are local to the block that they are declared in whereas bindings declared with var, are visible throughout the whole function that they appear in as well as the global scope, if they are not in a function

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

Functions

  1. function nameFunc(parameter1,parameter2){Body Statements;}
    OR
    var nameFunc = function(parameter1,parameter2){Body statements; }

  2. Var declarations are globally scoped or function locally scoped. Let is Block scoped and can be updated but not RE-DECLARED. So you can declare it only once in it's scope (in contrary of VAR).

  3. Pure Functions are Deterministic! They only care about their input they keep state local. they don't reference any external values.
    They will always produce same output based on its input.
  1. var someFunction = function(x,y){
    return x+y;
    }

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

    Both would be called with: someFunction(2,4); 6.

  2. Let has more restriction score. let x = 10 in a function cannot be used outside of that function. var y = 20 can be used outside of the function as well as inside it, however.

  3. A pure function will always work on the given parameters no matter what. It does not act on any global variables.

How can you create a function in Javascript?

There are many ways to create a function in Javascript

// Arrow functions
const multiply = (value1, value2) => value1 * value2;

// Function keyword
function multiply (value1, value2) {
  return value1 * value2;
}

// Creating an anonymous function with Function keyword
const multiply = function (value1, value2) {
  return value1 * value2; 
} 

What is the difference between var and let in regards to scope?
With “var” the scope is on functions and with “let” the scope is on blocks.

What is a pure function?
It is a function which does not do side-effects and always you call it with the same arguments, you receive the same result.

  1. 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. let is local to the block they are declared in, so it can only be used within that block. var is global, so it can be used throughout the whole function it is declared in, or if not in a function, it can be used globally.

  2. The pure function is a value-producing function, it has the property that, when called with the same arguments, it always produces the same value.

How can you create a function in Javascript?
faction Name_of_function( inputs) {
calculations;
return output;
}
What is the difference between var and let in regards to scope?
let is local to the block that it is declared in. var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.
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 code

How can you create a function in Javascript?

There are three ways (taken from eloquent javascript), I use the second most of the time.

let launchMissiles = function() {
missileSystem.launch(“now”);
};

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

const horn = () => {
console.log(“Toot”);
};

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

As of javascript 2015, regardless of where a var is declared, it is considered a global variable. A let has standard scope and operates similarly to the pre-2015 javascript var declaration which is local to the block it is declared in.

What is a pure function?

A pure function is a function that has no side effects and always returns the same value regardless of arguments.

1 Like

How can you create a function in Javascript?
using the keywork function. When spcifying
i.e.:
const function = myFuntionName(){
//body
};
OR
let function = myFuntionName(){
//body
};
OR WITHOUT CONST OR LET (no trailing ; at the end of the function body)
function future() {
return “You’ll never have flying cars”;
}
OR USING =>
const square1 = (x) => { return x * x; };
const square2 = x => x * x; // because its a one liner the curly brackets are not required.

What is the difference between var and let in regards to scope?
bindings created with “var” inside a loop are “visible” in the global scope whilst this is not true for bindings created with “let”.
With regards to functions both let and var are only visible in the local scope of the function.

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 code.”