Functions - Reading Assignment

How can you create a function in Javascript?
To create a function we can use a function declaration. The function keyword goes first, then goes the name of the function, then a list of parameters between the parentheses (comma-separated, empty in the example above) and finally the code of the function, also named “the function body”, between curly braces.

function myFunction(p1, p2) {
  return p1 * p2;   // The function returns the product of p1 and p2
}

What is the difference between var and let in regards to scope?
The main difference is the scope difference, while let can be only available inside the scope it’s declared, like in for loop, var can be accessed outside the loop for example. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

What is a pure function?
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.

1 Like

To create a function, we have to use the keyword function followed by a name, parentheses and the body of the function.
function name(parameters of the function){
code that is executed when the function is called
}

A binding that is defined within a block of code using let, is only visible within that block, it is called local variable. If we define a variable using var, that variable will be global, and seen by the whole program.

Pure function is a function that executes without side effects and executes the same way each time it is called, returning the same outputs for the same inputs.

1 Like

Thank you for bothering to format your code correctly.
please continue like that :+1:
Ivo

1 Like
  1. How can you create a function in Javascript?
    You create a function with 1st an expression starting with the keyword function. Then you set the return parameters

ex. const square = function(x) {
return x* x;
}
2. What is the difference between var and let in regards to scope?
Let is a local binding while var is global.

  1. What is a pure function?
    A pure function is a value-producing function that has no side effects and doesn’t read global bindings whose value might change.
1 Like
  1. How can you create a function in Javascript? use the keyword function followed by a name, parentheses and the body of the function.
    2.What is the difference between var and let in regards to scope? var is global and let is locally scope.
  2. What is a pure function?it gives an output that is the same as the input, without side effects
1 Like
  1. How can you create a function in Javascript?
    By declaring it ex function functionName(parameters) { code to be executed}

  2. What is the difference between var and let in regards to scope?
    let statements are local to the block in the declaration where var is visible through the whole function

  3. What is a pure function?
    A function that has no side effects or side effects from other functions

1 Like
  1. ***How can you create a function in Javascript?
    A. A function is an expression that starts with the keyword, Function. The function body must always be wrapped in brackets.

  2. ***What is the difference between var and let in regards to scope?
    A. The difference between Var and Let is scope. Scope refers to the level of accessibility of variables. Scope determines when variables are visible in our script.

  3. ***What is a pure function?
    A. A pure function is a specific value producing function that does not have side effects and doesn’t rely on side effects from other code…

1 Like

Hi everyone!

Please, remember to use the preformatted text for your code snippets.


It may be useless if not formatted properly. :hugs:

Ivo

  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, if they are created inside of a loop, the code before and after the loop cannot see it. Bindings created with 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—for example, it doesn’t read global bindings whose value might change.

1 Like
  • How can you create a function in Javascript?
    One must call function and name it, followed by parentheses and any necessary parameters, and the code to be executed i.e.
    function n(a1, a2) {
    return a1 + a2;
    }

  • What is the difference between var and let in regards to scope?
    Var definitions have a global scope throughout the code, let allows one to specify a variable with a scope limited to its ecosystem.

  • What is a pure function?
    A pure function is a special form of function that is not ever affected by other variables in the code, nor does it produce any side effects.

1 Like
  • How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function, they have a set of parameters and a body which contains the statements that are to be executed.

  • What is the difference between var and let in regards to scope?
    They behave differently:
    var bindings end up in the nearest function scope or the global scope
    let bindings end up in the local scope and can not be seen outside of it

  • What is a pure function?
    A specific kind of value producing function with no side effects and it does not rely on side effects from other code. when called with the same arguments it always produces the same value.

1 Like

1. How can you create a function in Javascript?

As the book says " A function definition is a regular binding where the value of the binding is
a function. A function is created with an expression that starts with the keyword function"

A function can be written multiple ways. Bellow you can fin the most common ways. (I think…)

a. Demo = function(x) {
body of function;
}

b. function Demo(x){
body of function;
}

When calling the function, we will write: Demo(x)

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

Variable VAR has a flaw in it and is being solved using constants LET and CONST. It is not completely replaced because there are multiple websites that have been built using the variable VAR, which will lead those websites to fail.

If the VAR variable is defined in a FUNCTION’S LOCAL SCOPE, it CAN’T be accessed outside of it, in the GLOBAL SCOPE.

function example(){
var a = 2;
}
 
console.log(a)

Uncaught ReferenceError: a is not defined
    at <anonymous>:1:13

If the VAR variable is defined in a BLOCK’S LOCAL SCOPE, it CAN’T be accessed outside of it, in the GLOBAL SCOPE.

if(true){
var a = 2;
}
 
console.log(a)
2

When using the variable LET inside the scope of a BLOCK or FUNCTION, it CAN’T be accessed in the GLOBAL SCOPE

if(true){
let a = 2;
}
 
console.log(a)
 Uncaught ReferenceError: a is not defined
    at <anonymous>:1:13

function example(){
let a = 2;
}
 
console.log(a)
 Uncaught ReferenceError: a is not defined
    at <anonymous>:1:13

**3. What is a pure function?**

When a function has No Side Effects and it is Deterministic - It is called a PURE FUNCTION.
A deterministic function it’s a function that will always produce the same output for a particular input.

3 Likes
  1. by defining it e.g. const example = function(){};
  2. let can be local but var is always global.
  3. value producing function
1 Like
Questions
  1. How can you create a function in Javascript?
  2. What is the difference between var and let in regards to scope?
  3. What is a pure function?
  1. A JavaScript function is defined with the function keyword, followed by a name , followed by parentheses () .
    Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
    The parentheses may include parameter names separated by commas:
    ( parameter1, parameter2, … )
    The code to be executed, by the function, is placed inside curly brackets: {}

  2. let gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var .
    var is rather a keyword which defines a variable globally regardless of block scope.


    let variables are usually used when there is a limited use of those variables. Say, in for loops, while loops or inside the scope of if conditions etc. Basically, where ever the scope of the variable has to be limited.
    For loop using let variable:

for(let i=0;i<10;i++){console.log(i); //i is visible thus is logged in the console as 0,1,2,....,9}
console.log(i); //throws an error as "i is not defined" because i is not visible

For loop using `var` variable:
for(var i=0; i<10; i++){console.log(i); //i is visible thus is logged in the console as 0,1,2,....,9}
console.log(i); //i is visible here too. thus is logged as 10.


As you can see the var variable is logged as 10 outside of the for loop too.

It logged it as 10 because the for loop terminates after checking the incremented value of i.

let variables cannot be re-declared while var variable can be re-declared in the same scope.

var temp = "this is a temp variable";
var temp = "this is a second temp variable"; //replaced easily

We cannot do the same thing with let

let temp = "this is a temp variable";
let temp = "this is a second temp variable" //SyntaxError: temp is already declared

  1. The pure 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 pure function does not produce any observable side effects such as network requests, input and output devices, or data mutation.
2 Likes
  1. How can you create a function in Javascript?
    A function is created when you make an expression starting with the keyword “function”.

  2. What is the difference between var and let in regards to scope?
    A ‘var’ declaration is visible throughout the whole of the function or globally if declared outside of a function whereas a ‘let’ declaration is specific to block of code where it is cited.

  3. What is a pure function?
    A function that has no external dependencies from outside of the function itself. You should be able to run it in isolation and should work.

1 Like
  1. There are several formats for defining a function in Javascript:
    a. let myFunction = function (arg1, arg2) { /* function body here / };
    b. function myFunction (arg1, arg2) { /
    function body here / };
    c. const myFunction = (arg1, arg2) -> ) { /
    function body here */ };

  2. var defines a global binding, either global to the whole program scope or global to the nearest enclosing function. let (and const) is always local in scope to the block in which they are declared.

  3. A pure function is one that returns a value, produces no side effects and the function isn’t dependent upon side effects from other code (e.g. use global variables). Therefore a pure function when called with the same arguments, will always produce the same return value.

3 Likes

Amazing answer sir! keep it like that! :slight_smile:

Carlos Z.

1 Like

1.function square(a) return a*a
That means you write the word function, then write your function name and give in following brackets the input parameters. There is no need for defining input parameters. You can define functions without any input parameters.

  1. When you define something with let it is just living inside a function or block. Let is so to say not global which means the variable declared with let only lives inside the block/function. You cannot call or assign something to it outside this block. Whereas a definition of something with var can anytime be used in any block.

  2. A pure function only calculate some values and therefore has no side effects and is not relying on some else piece of code. A good example is the function square I defined in my answer number 1. There is just a calculation of the square number, nothing more.

1 Like

1. How can you create a function in Javascript? A function is created in JavaScript with an expression that starts with the keyword. Functions also have parameters and a body which contain the statements that are to be executed when the function is called.
2. What is the difference between var and let in regards to scope? Bindings declared with let are local to the block they are declared in. Old style bindings created with the var keyword, are visible throughout the whole function they appear in, or the global scope if they are not in a function.
3. What is a pure function? It is a value producing function that doesn’t rely on side effects from other code and doesn’t have side effects. It doesn’t read global bindings who’s values might change.

1 Like

hello sir, you are using the wrong topic for your exercises, although they are working properly, please delete that post and then post then Here so we can have the proper order of assignments! :slight_smile:

Just copy/paste your code in the proper topic, because their all good my friend! keep it like that!
Chapter 2 Exercises.

Carlos Z.

1 Like