How To Learn Javascript Properly
Week 2 – Chapter 3


Since there’s so much to cover in week 2,  I felt the need to break it up into 2 different post.  Professional JavaScript for Web Developers

Here’s what I’ve learned just in chapter 3:

OPERATORS

  • “The logical NOT operator can also be used to convert a value into its Boolean equivalent.” so for example
    alert(!!"blue"); //which results in true

    is the same as writing

    alert(Boolean("blue");
  • You can use “OR” operators in variables.  For Example:
    var result = true || false;
  • Unary operators initiate based on it’s location to the variable. For Example:
    var num1 = 2;
    var num2 = 20;
    var num3 = num1-- + num2; //equals 22
    var num4 = num1 + num2; //equals 21
  • When performing comparison operators on strings it checks the value of the first letter in the string and capitals are of lower value then lower case letters.
  • When comparing numbers that are in strings, the js interpreter will only convert them to integers if one of the two operands are an integer.  For example:
    var result = "23" < "3"; //true
    var result = "23" < 3; //false
    

STATEMENTS

  • Funny how you think you know something till you get a better understanding of it…. do while statements will always execute the code block at least once before the expression is evaluated.  (How I never retained that is sad).
  • Object properties in ECMAScript are unordered, so the order in which property names are returned in a for-in statement cannot necessarily be predicted. All enumerable properties will be returned once, but the order may differ across browsers.
  • Playing around with break and continue statements I ended up playing around with nested loops.  I ended up writing this to help my brain wrap around the way the loops cycle through.
    var num = 0;
    outermost:
    for (var i=0; i < 10; i++) {
    console.log("the outer loop is now at " + i);
    for (var j=0; j < 10; j++) {
    if (i == 5 && j == 5) {
    continue outermost;
    }
    console.log("the inner loop is now at " + j);
    num++;
    }
    }
    alert(num);
  • With loops are used on objects for efficiency, however they’re considered poor practice due to negative performance and debugging difficulties.
  • Writing a function that sometimes returns a value causes confusion, especially during debugging.
  • Functions doesn’t care how many arguments are passed in and the arguments can be called from within the function using the arguments[] object – There is alot to cover regarding how it works with named arguments and what not but I think it is incredibly powerful and useful. Here’s an example of something I wrote demonstrating this with the length method:
    function sayHi() {
    for(i=0; i<arguments.length; i++){
    if(arguments[i] == "This is what I'm looking for!"){
    alert(arguments[i]);
    continue
    }
    console.log(arguments[i]);
    }
    }
    sayHi("to this ","being concatenated", "and here", "This is what I'm looking for!",
    "is some", "more arguments", "to test" );
  • Function overloading is not possible because of the lack of function signatures.