Common Language Constructs - jpjohnsonjr/learning-notes GitHub Wiki

Concatenation

Take this sample code:

 1 // ***** String concatenation
 2 var string = "Hello";
 3 string += " World";
 4 console.log(string + "!");
  • Line 2 declares the variable string and sets the value as "Hello".
  • Line 3 adds to that string the value " World". Note that the operation string += is equivalent to string = string + " World"
  • Line 4 concatenates in the output without changing the value of the declared variable.

Math operators

Standard as in most programming languages: +,-,*,/. Precedence of parentheses also applies. For example, console.log((5 + 4) / 3); will output the value 3 to the console.

An operation such as console.log(undefined / 5); will yield the output NaN to the console, which stands for "not a number". This would usually be an indication of a bug -- something that was supposed to be passed along but was not.

Equality

Take the following sample code:

22 var x = 4, y = 4;
23 if (x == y) {
24   console.log("x=4 is equal to y=4);
25 }
26 
27 x = "4";
28 if (x== y) {
29   console.log("x='4' is equal to y=4");
30 }

Note shortcut in Line 22, declaring two variables at the same time. Line 23 compares value of x to y using double equal sign. Common mistake is to use only a single equals. If that is done, would take value of y, place it in x, and use the result as the value to evaluate whether true or not.

Line 27 sets value of x as a string rather than as a number 4. Lines 28-29 demonstrate concept of "type coercion." If using comparison such as x==y when there are two different variables, it will change the types to be like each other.

Strict equality

Take for example the following code:

37 if (x === y) {
38   console.log("Strict: x='4' is equal to y=4");
39 }
40
41 else {
42   console
43   .log("Strict: x='4' is NOT equal to y=4");
44 }

Strict equality, using triple equals, will NOT convert variable types.

Boolean TRUE versus boolean FALSE

If statement (all false)

Consider the following code:

51 if (false || null || 
52    undefined || "" || 0 || NaN) {
53   console.log("This line won't ever execute");
54 }
55 else {
56   console.log ("All false");
57 }

What Lines 52-53 mean:

  • Term "false" will always be evaluated as false
  • Double bars (||) are or symbol. If the first argument is evaluated as TRUE, processing stops. (In this case it continues.)
  • These values are always "coerced" to being false:
    • null
    • undefined
    • blank ("")
    • 0
    • NaN

If statement (all true)

Consider the following code:

60 if (true && "hello" && 1 && -1 && "false") {
61   console.log("All true");
62 }

Two ampersands serve as logical "AND" operator. If all statements get evaluated as true then, will all be true. Note that putting "false" in quotes makes it a string instead of a reserved word, and therefore true.

Best practices for {} style

In Javascript, the use of curly braces is not just a matter of style; it actually matters from a standpoint of syntax. Take the following example code:

71 function a()
72 {
73   return
74   {
75     name: "Yaakov"
76   };
77 }
78
79 function b() {
80   return {
81     name: "Yaakov"
82   };
83 }
84 
85 console.log(a());
86 console.log(b());

In the above code, function a will execute as "undefined" while function b will execute as Object {name: "Yaakov"}. The reason a executes as undefined is that Javascript does not find anything after return and therefore inserts (virtually) a semicolon after it.

For loop

90 var sum = 0;
91 for (var i = 0; i < 10; i++ {
92   sum = sum + i;
93 }
94 console.log("sum of 0 through 9 is: " + sum);

This will loop between 0 to 9 and sum everything through. Parts of the loop:

  • var i = 0 is the initialization
  • i < 10 is the condition
  • i++ is the increment (i++ is the same as i = i + 1)