Category Archives: Tips and Tricks

A collection of insights I’ve found helpful in my web development process.

What’s an Object in JavaScript?

Almost everything is an object.

  • Primitives: numbers, booleans, strings, null, undefined
  • Objects: Math, Date, JSON, window, document, objects you create, array, function, numbers, booleans, strings

Functions are “first class” values.

  • Assign a function to a variable.
  • Store a function as a value in an array or object.
  • Pass a function to a function.
  • Return a function from a function.

Primitives don’t look like objects.


var phoneNumber = "555-1212";	//phoneNumber is a primitive.
console.log(phoneNumber.length);//Now we need phoneNumber to be an object.
/*********************
*	BEHIND THE SCENES
*
*	phoneNumber is converted to a string object.
*	.length property is then accessed
*	
**********************/	
console.log(phoneNumber);		//phoneNumber is a primitive again.

Dos and Don’ts in JavaScript

Best Practices:

  • Choose short but readable variable names.
  • Avoid global variables.
  • Use only one global object to encapsulate any global variables you really need.
  • Always use var to declare your variables.
  • Indent your code so it’s readable.
  • Use curly braces to define blocks of code.
  • Comment your code.
  • Always use semi-colons.
  • Be aware of where automatic semi-colon insertion happens.
  • Declare variables outside loops.
  • Reduce DOM operations.