Sunday, August 31, 2014

Conditions and Loops

Checking if a Variable Exists

>>> if (typeof somevar !== "undefined"){result = 'yes';}

variable somevar may have been declared but not assigned a value yet and you'll still get the same result

>>> var somevar;
>>> if (typeof somevar !== "undefined"){result = 'yes';}
>>> result;
""

typeof will always return a string and you can compare this string with "undefined"

>>> var somevar;
>>> if (typeof somevar !== "undefined"){result = 'yes';}
>>> result;
""
>>> somevar = undefined;
>>> if (typeof somevar !== "undefined"){result = 'yes';}
>>> result;
""

Alternative if Syntax

var result = (a === 1) ? "a is one" : "a is not one";

Switch

var a = '1';
var result = '';
switch (a) {
case 1:
result = 'Number 1';
break;
case '1':
result = 'String 1';
break;
default:
result = 'I don\'t know';
break;
}
result;

Loops

In JavaScript, there are four types of loops:

while loops
do-while loops
for loops
for-in loops

var punishment = '';
for (var i = 0; i < 100; i++) {
punishment += 'I will never do this again, ';
}

var a = [ 'a', 'b', 'c', 'x', 'y', 'z'];
var result = '\n';
for (var i in a) {
result += 'index: ' + i + ', value: ' + a[i] + '\n';
}


Arrays

>>> var a = [];
>>> typeof a;
"object"

>>> var a = [1,2,3];

Adding/Updating Array Elements

>>> a[2] = 'three';
"three"
>>> a
[1, 2, "three"]

>>> var a = [1,2,3];
>>> a[6] = 'new';
"new"
>>> a
[1, 2, 3, undefined, undefined, undefined, "new"]

Deleting Elements

>>> var a = [1, 2, 3];
>>> delete a[1];
true
>>> a
[1, undefined, 3]

Arrays of arrays

>>> var a = [1, "two", false, null, undefined];
>>> a
[1, "two", false, null, undefined]
>>> a[5] = [1,2,3]
[1, 2, 3]
>>> a
[1, "two", false, null, undefined, [1, 2, 3]]

>>> var a = [[1,2,3],[4,5,6]];
>>> a
[[1, 2, 3], [4, 5, 6]]

>>> a[0]
[1, 2, 3]

>>> a[0][0]
1

>>> var s = 'one';
>>> s[0]
"o"

Primitive Data Types

Variables are Case Sensitive

Primitive Data Types

Number

  • this includes floating point numbers as well as integers
  • Infinity
  • NaN
infinity - infinity is NAN
10 * "f" is NAN
1 + 2 + NAN is NAN

String
var s1 = "one";
var s2 = "two";
var s3 = s1 + s2;
s3 is onetwo

Boolean
  • can be either true or false
If you quote true or false, they become strings
Understanding how any value converts to a boolean is important. The following values convert to false
  • The empty string ""
  • null
  • undefined
  • The number 0
  • The number NaN
  • The boolean false
Undefined
when you try to access a variable that doesn't exist, you get the special value undefined.
The same will happen when you have declared a variable, but not given it a value yet.
>>> var i = 1 + undefined; i;
NaN
>>> 1*undefined
NaN
>>> !!undefined
false
>>> "" + undefined
"undefined"

Null
It means no value, an empty value, nothing.
The difference with undefined is that if a variable has a value null, it is still defined, it only happens that its value is nothing.
null is an object

>>> var somevar = null
>>> typeof somevar
"object"

>>> var i = 1 + null; i;
1
>>> 1*null
0
>>> !!null
false
>>> "" + null
"null"

typeof Operator

To know the data type of a variable or a value, you can use the special typeof operator.
This operator returns a string that represents the data type.

Logical Operators

!, &&, ||

Order of precedence (!, &&, ||)

Oddities

if JavaScript encounters a non-boolean expression as an operand in a logical operation, the non-boolean is returned as a result.

>>> true || "something"
true
>>> true && "something"
"something"

Comparison

==
===
  • both value and type equality

!=
  • Returns true if the operands are not equal to each other (after a type conversion)

!==
  • Non-equality comparison without type conversion
>
>=
<
<=

>>> NaN == NaN
false