Functions - Reading Assignment

  1. Function in JavaScript is created by an expression with the keyword function followed the parameter and the body, which contains the statement to be executed when the function is called.
    const square = function (x) {
    return x * x;
    };

  2. var is a global binding
    let is a local binding.

3.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.

1 Like
  1. Bind the function into a variable; use a function declaration, use arrow functions
  2. let is defined locally to a block whereas a variable declared with var can be seen everywhere in the block that it was defined
  3. A pure function is a function without side effects and it’s output for the same input is persistent no matter what.
1 Like
  1. A function is created with an expression that starts with the keyword function. Ex: const doublex= function(x) { return 2 * x; };
  2. Bindings declared with 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 the loop cannot “see” it. Bindings created with the 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. 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

I’m having a hard time understanding how this “function” programming actually works line for line but when I try the examples in the book, my console does not return the values the way the book says it should. What am I doing wrong?

image

The square function works just as you wrote it, in both ways. it even works if you just type directly square (x) in console. For power function you did not close the body of the function, you need } at the end.

2 Likes
  1. By creating an expression with the keyword “function”. Setting the parameter by using braces.
  2. “let” is local to the block it was created in. If “var” is place outside of a function, it appears is the global scope.
  3. A specific value producing function that has zero side effects and doesn’t rely on side effects from other code.
1 Like

Hi @CryptoDance,

Just don’t console.log the function call. When you console.log the function call, console displays the execution return statement which would not yield an answer. Just do square(12) at the end and you will see an answer as you are returning it inside it.

2 Likes
  1. You can create a function using the keyword “function” and having parameters inside the “()” followed by the body which is written inside the “{}”, this contains the statements that the function will execute when called upon.

  2. A let (and const) can have a local scope when placed within the block of the function, this means that the bindings created cannot be recognized from outside the function. A var is always global in scope (I think, please correct me if I’m wrong), and cannot be a local scope. A let binding can also be a global scope when placed outside of the function.

  3. A pure function is a specific type of Value-producing function with no side effects and does not rely on the side effects of other codes. Basically, it always produces the same value and nothing else.

2 Likes

How can you create a function in Javascript?

A function is created with an expression that starts with the keyword function

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

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.
If you create one of those inside of a loop, the code before and after the loop cannot “see” it.
Bindings created with the var keyword are visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a 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.
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).

2 Likes
  1. const function_name = function([arguments]) {};
  2. If var is declared inside a scope, it is visible outside the scope as well whereas let is only visible inside the scope it is declared.
  3. A pure function is one that is stand alone and does not rely on external values to return a value. It can be tested on its own.
2 Likes
  1. How can you create a function in Javascript?
  • A function is created with an expression that starts with the keyword
  1. What is the difference between var and let in regards to scope?
  • Scope is the part of the program in which the binding
    is visible. Let is local; not visible outside its loop or function.
    var is global; visible throughout the program.
  1. 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.
2 Likes
  1. How can you create a function in Javascript?
    function functionName(parameters) {code to execute}

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

  3. What is a pure function?
    A pure function is a value producing function with no side effects and has no reliance on other code with side effects.

function one (x){
return x+1
}

2 Likes
  1. function nameFunc() { code to execute}
    2.let and const are able to be local to the block that they are in and var is a global to program.
  2. a pure function is a well written function with no side effects
2 Likes
  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. LET is a binding that is local to the block that they are declared in
    VAR bindings are visible throughout the whole function they appear in or throughout the global scope if they are not in a function

  3. A pure function is a function with no side effects and is not reliant on other code

2 Likes
  1. How can you create a function in Javascript?
    A function is created with an expression that starts with the keyword function and adding parameters and body
  2. What is the difference between var and let in regards to scope?
    LET is only visible as local binding whereas Var is global
  3. What is a pure function?
    Function relying only on itself
2 Likes

Think about the following questions while you are reading:

  1. How can you create a function in Javascript?

In a simple language,to make a function we need a code starting with the keyword function followed by a parameter which is wrapped in the parentheses

as well as open/close brackets which holding the body of the code that are going to be executed after calling it at the end of the code.

meaning : 1-function 2-name the function(camelcase) 3-(parameters)4-{body of code} ex:function(var no1,var no2,….){set of codes}

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

var can be applied locally or globally to the block so it has a larger scope and its visible throughout the global scope,

while let is visible whiten the function and its been designed for local bindings only.

  1. What is a pure function?

the functions that have no side effects, and even doesn’t rely on side effects and just produces the value and dent modify the variable

outside it’s scope.
:smiley:

2 Likes
  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. “let” are usually used when there is a limited of use of those variables, for example, in for loops, while loops or inside the scope of conditions. “var” is used when value of variable need to be less changed and used to be accessed globally.

  3. Pure function are similar to mathematical functions. For any given input, a pure function must return exactly one possible value.

2 Likes
1. How can you create a function in Javascript? By wrapping a program in a value. It’s created by an expression that starts with a keyword followed by the body which has a statement that is executed when called upon.  Or by declaring it at the start of a statement. Another way is =>.
2. What is the difference between var and let in regards to scope? Let has a binding that is local to the scope like const. Var has a binding that is global for the entire program. 
3. What is a pure function? A value producing function that has no side effects nor does it require a side effect. A pure function always will produce the same value when called upon.
2 Likes
  1. A function is created with an expression that starts with the keyword function. Functions have a set of parameters (or no parameters at all) and a body, which contains the statement that are to be executed when the function is called. An example code could be:
const square = function(x){
   return x * x;
}
  1. Let variables are limited in scope of block (inside the curly parenthesis) while var variables can be used inside the function no matter how deep the variable was defined in its block.

  2. A pure function simply takes in input(s) and returns an output without producing any side effects. An example could be, f(x)=x^2.

2 Likes
  1. How can you create a function in Javascript?
    A function is created by giving it a name, parameters, and code to be executed.

  2. What is the difference between var and let in regards to scope?
    Var defines a global variable while let defines a variable within a certain block making it local to that specific block of code.

  3. What is a pure function?
    A pure function is a function that has no side effects and doesn’t rely on side effects from other code

2 Likes