Functions - Reading Assignment

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

1 Like
  1. How can you create a function in Javascript?
    Function keyword, Name of function, Function parameters in parenthesis, Code of function between curly braces

eg.

function welcomeMessage () {

alert(“welcome everyone”);

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

Var is used for the global environment and let is used for the block scope of a function

  1. What is a pure function?

A pure function is a function that always returns the same value when the same input is given and produces no side effects

1 Like

The concept is very abstract for me so far, good thing is that I’m learning as I go. I think I understood.

Thanks for you review. :+1:

2 Likes

Hi @Jody,

Nice answers :ok_hand:

Just a comment about Q2


In fact, variables declared with let are also available globally if they are declared outside of all code blocks and functions.

Take a look at this post for some clarification. And if you would like to go a bit deeper, then try this post.

1 Like

Hey @kmilo_Mart!

Don’t worry, the differences between var and let are difficult to grasp at first



take a look at this post for some further clarification. And if you would like to go a bit deeper, then try this post. :slightly_smiling_face:

2 Likes
  1. There are three ways to create a function in Javascript:
  • const functionName=function(var_1,
,var_n){
 (function body)}
  • function functionName(var_1,
,var_n){
 (function body)}
  • const functionName=(var_1,
,var_n){
(function body)}
  1. Variables that are initialized with “let” are visible only within their local code block. By contrast, variables that are initialized with “var” are globally visible either within a function or within the program as a whole.
  2. A function is said to be “pure” if it produces its output from its input in a perfectly self-contained fashion without producing external side effects and without utilizing external information from other program parts.
1 Like

How can you create a function in Javascript?
You assign the function to a variable(with parameters) and {execute the instructions within the curly braces}
const function = function(parameters){instructions to be executed}

What is the difference between var and let in regards to scope?
Bindings declared with let 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. 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

1 Like

var x = function(i) {return i*i};

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

var x = i => i*i;

var is global, let is local

“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
  1. How can you create a function in Javascript?
    a. with the function keyword: function function_name(arguments*) { func_body
}
    b. const function_name = function(arguments) {func_body};
    c. arrow function: const function_name = (arguments) => { function_body};

  2. What is the difference between var and let in regards to scope?
    let declares bindings to be local to the block; while var bindings are global scope, or visible throughout the whole function that they appear in.

  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. How can you create a function in Javascript?
    Functions are created using a keyword and then setting parameters to the function,
    = function().

  2. What is the difference between var and let in regards to scope?
    The binding let limits the scope of the function to the block.

  3. What is a pure function?
    A pure function is a specific function that produces value. It creates no side effects and uses no side effects created by other code. It reads no value that may be subject to change.

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

    Bind something to an expression with a set of parameters & call it with the command “function”

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

    As far as scope var = is a global binding while let = is expression specific

  3. What is a pure function?

    A pure function is one that has none & relies not on “side effects”. You call it. It functions regardless of environmental influences.Inout values rule the day!(respectively)

1 Like
  1. How can you create a function in Javascript?
  • function functionName(parameters) {Code to execute}
  1. What is the difference between var and let in regards to scope?
  • Var is for global bindings and let is used for local bindings
  1. What is a pure function?
  • It wont have any side effects and does not rely on side effects from other code
1 Like

Hi @FrankB,

You’ve missed out the arrow in your arrow function example:

const functionName = (var_1, ..., var_n) => {...}

Excellent answers @KingArt :star2:

Hi @CeesQ,

They can be, but they can each have other types of scope as well, so that isn’t the difference. The differences between var and let are difficult to grasp at first



take a look at this post for some clarification. And if you would like to go a bit deeper, then try this post.

Hi @DeezSats,

You are part of the way there, but you’ve omitted some important details, such as the function name, and the function body enclosed in curly brackets.

You seem to be confused here
 let isn’t a binding; its a keyword declaring a binding. It defines the scope of the binding, not a function


 and how is let different to var ?
The differences between them are difficult to grasp at first, but take a look at this post for some clarification. And if you would like to go a bit deeper, then try this post.

1 Like

Hi @AliasFresh,


 your description is confusing, to be honest. You can bind the function to a variable, yes; but either way you normally need to use the keyword function, give the function a name, and, as you’ve said, a set of optional parameters enclosed within parentheses. You then also have the function body enclosed in curly brackets, which contains the block of code that will be executed when the function is called. You don’t call the function with the keyword function (this keyword defines (or declares) the function. You call a function by appending parentheses to its name and placing the arguments (which become the parameters) within the parentheses.

I hope that’s now clearer.

A binding declared with var can have global scope, but not always. And let isn’t expression specific, but specific to the code block it is declared in.

The differences between var and let are difficult to grasp at first, but take a look at this post for some further clarification. And if you would like to go a bit deeper, then try this post.

1 Like

Hi @Cryptojunkie,

Q1 & 3 :ok_hand:

They can be, but they can each have other types of scope as well, so that isn’t the difference. The differences between var and let are difficult to grasp at first, but take a look at this post for some clarification. And if you would like to go a bit deeper, then try this post.

2 Likes

Oh my
 I meant to say let limits the scope of the binding to the block.
geez what I wrote doesn’t even match my notes.
I’ll check out the post to be sure I have it proper.

1 Like

Ok those posts helped for better understanding.

1 Like