Functions - Reading Assignment

How can you create a function in Javascript?

There are different ways to define functions in JavaScript

  1.        const myFunction = function(argument1, argument2) {
             return "I recieved " + argument1 + " and " + argument2 + "."
            }; // this method assigns value of function to a binding
    
  2.        function myFunction(argument1, argument2) {
             return "I recieved " + argument1 + " and " + argument2 + "."
           } // This is a function being declared and does not need to exist before being called. Requires end semi-colon
    
  3.       const myFunction = (argument1, argument2) => {
              return "I recieved " + argument1 + " and " + argument2 + "."
           }; // Arrow function which requires to end with the semi-colon
    

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

When bindings are declared using the var keyword they are global in nature and can be seen from all parts of the program even if called inside a function. let is more specific than var and gives more precision as to the scope of the binding being declared. let can be global if declared in the main body of a program but if bindings are declared with let inside a for loop for example that binding can only be seen inside that loop and not from outside. The same if let declares a binding inside a function. That binding is not visible from outside the function but is visible to functions inside the parent function that declares the binding.

What is a pure function?

A pure function is a function that does not depend on any side effects nor does it depend on any bindings outside of its self. The function should always be able to return the expected value no matter the state of the current running environment.

1 Like

Answers

  1. You can create a function in JavaScript by creating an expression that starts with the keyword function.

  2. When using let keyword within a block its scope limit is only limited in where it was used as in Var keyword can continue to scope outside a block.

  3. A pure function accept an input and returns a value without modifying any data outside the scope.

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

Function can be declared with the keyword function. For example:

function addValues(value1, value2) {
  return value1 + value2;
}

Fuction addValues can then be called for example like this:

let addedNumbers = addValues(1, 2);

Or function can be defined using an expression:

const addValues = function(value1, value2) {
  return value1 + value2;
};

The binding can then be used as a function:

console.log(addValues(1, 2));
  1. What is the difference between var and let in regards to scope?
    Binding created with keyword let is visible only in the block where it is declared. Bindings created with var are visible in the whole function.

  2. What is a pure function?
    Pure functions are functions that don’t use or modify any data outside their scope. They always return a value.

1 Like

Nice answers, Luis :ok_hand:

Just an additional comment about Q2…

This is also true with let and const 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

Nice detailed answers again, @daz3d! :+1:

Great analysis of the different ways to create a function :star2:
Just an additional observation, as I can see you’re really interested in the detail:
You’ve said that the function declaration (your method 2) requires a semi colon at the end — that’s not true. I think maybe it’s just a slip and you meant to put this for your method 1.
The whole JavaScript semi colon debate is really frustrating to be honest. It seems that most of the semi colons at the end of statements can now be left off based on more recent JavaScript guidelines. However, in practice there seems to be an annoyingly mixed approach to their use. From what I understand (but this might not be the most technically accurate or purist of explanations), if you are putting semi colons at the end of your bindings, then it makes sense to put them at the end of your function expressions which are assigned to bindings. So this would apply to your 1st and 3rd method, but not to your 2nd method. But I really wouldn’t lose any sleep over it, and I seem to find that (when it comes to using them at the end of bindings, statements and code blocks wrapped in curly braces) all JavaScript variations on the theme of the semi-colon seem to work ok :sweat_smile:
(Warning - there are still specific cases when semi-colons MUST be used e.g. after the first two expressions in a for loop header.)
I think the main thing is to be consistent in your practice, especially within the same program, otherwise it can be frustrating for another developer reading or working with it, and could even lead to confusion.

Great effort with Q2. It’s really difficult to be honest. You seem to have let nailed, but you may want to check your understanding about var.
Take a look at this post for some clarification. And if you would like to go a bit deeper (I know you would…) then try this post.

1 Like

Great analysis, @tmalk!..especially of how to create a function :muscle:

1 Like

Hi @cecilia!

Q1 & 3 :ok_hand:

Question 2…

Yes :+1:

No … variables declared with var are still limited, but to the function scope they are declared within (rather than the block scope that let variables are limited to).
This is complicated. Take a look at this post for further clarification. And if you would like to go a bit deeper, then try this post.

Keep on learning! :muscle:

1 Like

1 with an expression that starts with the key word function, function have a set of parameters and a body, which contains the statement that are to be executed when the function is called

2 let is used local, only vissable in the block that it is declared in, var is vissible in the whole code.

3 a pure function is a specific kinde of valueproducing function that not only has no side-effects but also doesnot rely on side effects from other code.

1 Like

How can you create a function in Javascript?

  • A function definition is a regular binding where the value of the binding is a function.
  • For example, this code defines square to refer to a function that produces the square of a given number:

const square = function(x) {

return x * x;

};

console.log(square(12));

// → 144

  • A function is created with an expression that starts with the keyword function. Functions have a set of parameters (in this case, only x) 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.

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

image

What is a pure function?

  • A pure function is a specific kind of value-producing function that has no side effects and 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, always produces the same value (and doesn’t do anything else).
  • When you are not sure that a pure function is working correctly, you can test it by directly calling it and know that if it works in that context, it will work in any context.
1 Like

Thanks for the feedback @jon_m. The semicolon has been tricky for me to grasp also and when i jumped to the conclusion of the semicolon was from looking at the way the arrow and assignment functions were formed in the book. i will keep what you have suggested in mind and test it out as I move forward. But you are correct i didn’t intend to have the semicolon at the end of method 2. the only place i noticed need for semicolon was method 1 and 3.

Also regarding the use of var. i realized my error when i moved on to to the next video after the assignment and @ivan cleared that matter up for me. i should have come back to edit my post but thought it would be interesting to see if my mistake would be picked up.

Thank you for your diligence and for the clarity you bring to the exercises.

2 Likes

1. How can you create a function in Javascript?
It is created with an expression that starts with the keyword function followed by parameters in between parentheses and the body between braces that contain the statement(s).
2. What is the difference between var and let in regards to scope?
The scope of let is the block where they are declared in. If you use it in a loop for example they cannot be seen before or after the loop. Whereas var has the scope within a function or global for the entire program.
3. What is a pure function?
It is a specific kind of value-producing function that not only has no side effects but also does not rely on side effect from other code.

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.

  2. What is the difference between var and let in regards to scope?
    let is used locally, while var is used to define variables globally.

  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

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

Carlos Z.

How can you create a function in Javascript?
Here is the syntax to create functions in JS.
function fucntionName(parameter 1, parameter 2){function defination statement
}
functionName(argument 1, argument2);
What is the difference between var and let in regards to scope?
“Let” and “const” are used for local bindings where “var” is for global bindings.

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—for
example, it doesn’t read global bindings whose value might change

1 Like
  1. A function is created with the keyword function. Functions have a set of parameters and a body that contains the statements that are to be executed when function is called.

  2. Var is used outside of the block and can be used for global bindings where as let is used within the block.

3, A pure function is a function that has no side effects and produces the same outcome each time.

1 Like
  1. Type the keyword function followed by an expression and a block of code (aka the body) indicated with curly braces, which holds the program. The expression can have a single parameter, multiple parameters, or no parameters at all.
  2. var keyword, is visible throughout the whole function that they appear in—or throughout the global scope, if they are not in a function. Let keyword are in fact local to the block that they are declared in.
  3. A function where the value returned does not produce side effects and always returns the same result given the same parameters.
1 Like
  1. How can you create a function in Javascript?
    without function argument(s)
    functionName = function() {
    // function statements;
    // …
    };

with function arguments
functionName = function(arg1, arg2) {
// function statements;
// …
};

  1. What is the difference between var and let in regards to scope?
    The let is in local to the block that they are declared in, and the var visible throughout the whole function that they appear in—or throughout the global scope.

  2. 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. By using keyword ‘function’, then parameter ‘()’, then body ‘{}’.

  2. ‘var’ and ‘let’ are both used for variable declaration in JS, but the difference is that var is function scoped, and let is block scoped.

  3. A pure function is a function which:
    -Given the same input, will always return te same output.
    -Produces no side effects.

1 Like
  1. How can you create a function in Javascript?
    keyword function then a name then () and lastly { code to be exicuted }

function NameOfFunction(parameter1, parameter2) { return parameter1 + parameter2}

function add(x, y) {return x + y}

console.log(add(2, 5));

7

  1. What is the difference between var and let in regards to scope?
    var can be seen by the entire program. let can only be seen within the block they were created in.

  2. What is a pure function?
    A function that has no side effects and does not rely on any side effects from other parts of the code

1 Like
  1. By using the expression function
  2. There are local and global bindnings, when using let, the bindings within are local to the block they are created in, that is, not visible to the program outside. whereas var is visible thru the whole function they appear in.
  3. A function that produces value but no side effects
1 Like