7 - Why is there an "or" ( || ) in the addition?


Just wondering what this (highlighted in bold) is supposed to be in the original code:

add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;

If I'm not mistaken, this would mean that itemCost or 0 should be added to this.total ... but that does not seem to make much sense. After all, this is a simple mathematical addition, not an if statement. Neither do we want to work with booleans here, just with numbers. So what is this supposed to be and why is it there?



o6 = false || "Cat" // f || t returns "Cat"

You could do a test

var test = function(itemCost) {
    console.log( itemCost );
    var cost = itemCost || 0;
    console.log( cost );
test(); //Not using an argument will lead to =itemCost= being =undefined=

the undefined Value
will be Interpreted as a so-calloed falsy



the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**


**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


That explains it, thank you.



google search
== the Book ==
jquery [your question] site:developer.mozilla.org
CSS [your question] site:developer.mozilla.org
javascript [your question] site:developer.mozilla.org
[your question] site:jquery.com
[your question] site:getbootstrap.com

== discussions / opinions ==
jquery [your question] site:stackoverflow.com
CSS [your question] site:stackoverflow.com
javascript [your question] site:stackoverflow.com

== guidance ==
[your question] site:crockford.com
- - http://javascript.crockford.com/code.html
- - http://javascript.crockford.com/survey.html