Assorted, Scattered JavaScript Notes

Boolean(value) method returns false for falsey values, true for truthy values
falsy: false, null, undefined, “”, 0, NaN
truthy: everything else: “0”, “false”

+ as a leading operator converts a string to a number: +”42″ = 42. Same as Number(“42”) = 42), parseInt(“42”, 10) = 42
+”3″ + (+”4″) = 7

=== !== do not do type coercion, slightly faster

if (a) {
return a.member;
} else {
return a;
can be written as
return a && a.member;
if the first operand is truthy, return the second operand, else, return the first

but for || if the first operand is truthy, return the first operand, else, return the second:
var last = input || somethingElse;
so if input is truthy, then last is input, otherwise set last to somethingElse

you can label loops and break them by label:
myloop: for (whatever)
break myloop;

//other loop
for (var name in object) {
if (object.hasOwnProperty(name))        //avoids looking at objects we might have inherited from
name //is the current member
object[name] //is the current member value

//multiple conditions in switch statements
switch(something) {
case “;”;
case “,”;
case “.”;

//exceptions and object literals
throw new Error(myReason);

throw {         //create an “object literal” creates an exception object on the fly
name: myexceptionName,
message: reason

//object literals
an object literal is wrapped in { }
a name can be a names or a value
values are any type
: seperates names and values
, seperates pairs
object literals can be used anywhere a value can appear
var myObject = {name: “Jack”, ‘goto’: ‘Jail’, grade: ‘A’, level: 3};
var myName =;     //extracts “Jack”
var destination = myObject[‘goto’];     //extracts “Jail”
var destination = myObject[“name”];     //extracts “Jack”
var destination = myObject[goto];       //returns an error because goto is a reserved word, hence the other notation

//maker function
function maker (name, grade)
var it = {}; //makes a new empty object, good shorthand = name;
it.grade = grade;
return it;
var myObject = maker(“Jack”, “A”);

//an object with an object as a member, shorthanded
var myObject = {
name: “jack”,
grade: “a”,
clothes: {
pants: ‘blue’,
shirt: ‘red’

//if you have more than a couple parameters in a function, why not make them an object?
function myfunc(pa, pb, pc, pd, pe, pf)
myfunc(pa, pb, pc, pd, pe, pf)
function myfunc(specs)
myfunc({ pa: ‘1’, pc: ‘3’, pb:’2′, … })

** note: variables that are not var’ed become implicitly global

//specify object’s prototype on creation? linkage — doesn’t really exist
var newObject = object(otherObject);
//gives it a secret pointer to otherObject, but the secret pointer is used for retrieving only NOT storing
//object (lowercase) is a method that Yahoo made up
//different than

var newObject = Object() which equals: var newObject = object(Object.prototype)

delete myObject[memberName];    //deletes (sets to undefined) a member of an object

myArray[myArray.length] = 3     //appends to end of array (same as pop)

var myArray[]   //creates a new array?

Leave a Reply