In programming, variables refer to a storage location and an associated symbolic name which contains some known or unknown quantity or information (called the value).


There are really a lot of other places on the web for you to learn about the concept of a variable, so I’m not going to go into detail except to say:

  • EVERYTHING is an object (null, 3, “hello”, undefined, function something(){}, “A”, 4.3)
  • ALL numbers are 64 bit floating point (double), there aren’t “real” integers in the language
    • [related] Because of this, the bitwise operators (|, &, ^, >>, >>>, <<, <<<) are pretty pointless as they are very inefficient; requiring a conversion of the double to an integer, bit operation, then conversion back to a double
  • JavaScript is prototypal, object-oriented programming in JS may be a bit foreign if you are used to other populare OO languages like Java

First and foremost, you need to have a complete understanding of the supplied Objects in Javascript and the prototyping system as these are really the building blocks of the language. Because Javascript is a prototypal language, objects and classes can be extended at anytime by pretty much anything. This means you can modify classes and native objects on the fly which is both dangerous and cool (but mostly just dangerous). JavaScript has quite the bad name due to some of these, let’s call them “features”. Though it’s probably my favorite programming language, I have to concede to many of its opponents arguments; the language has some strange behavior, there are some really bad ideas that have withstood the test of time due to certain browser vendors being worried about breaking old applications,  and worst of all, the disintegrated platform (am I on Chrome? IE? Firefox? Opera?). Despite all of that, I still contend that JavaScript is a powerful programming language that is really a joy to work with once you understand the quirks and learn to avoid its weak spots.

Type System

JavaScript is a weakly typed language, meaning you do not have instruct a compiler as to what sort of value your variables will hold


<code>// all of this will run without error 
var some_variable = "I'm a string"; 
some_variable = 3.14159; 
some_variable = function(name){ return "Hello, " + name; }; 
some_variable = []; </code>


In contrast, strongly typed languages suce as C, C++ and Java require you to specify your variable types before using them, mostly for the purpose of memory management  (because the primitive types in those languages are allotted a specific number of bytes in memory) and data validation.

Default Objects and Classes

window – this is the global container or “Parent Object” for the program, all variables outside of a function body are bound (as attributes) to the window object in addition to variables within a functions scope that are not declared with the keyword varIt is imperative that you use var when declaring variables (including functions). Window has an arsenal of attributes and methods that I will go over in another article.

String [aka window.String] – “Hello”, new String(“Hello”), ‘H’. Yes, Strings are objects. “hello”.toUpperCase()

Number [aka window.Number (seeing a pattern yet?) – 3, 4.5, NaN (Not a Number), Infinity, -Infinity. Because variable and function names cannot begin with a number, 3.toString() is seen as invalid syntax, therefore we must use an expression to return the number (3).toString()

Object – String, Number, Window, {}, null, Array, everything else

Function – A function is a optionally-named code block that can have it’s own variable scope, can accept arguments and return values. (ex. function(){}, “Hi”.toString, function doSomething(){})

Boolean – Logical variable which can be either true or false

Date – a library of various Date/Time related methods

Math – you guessed it (ex. Math.pi, Math.abs(-1))

document – this is the container for the DOM and all of its objects

null – the absence of a value (also an object)

undefined – an odd fellow, but the value held in variables that have been initialized without a value (ex. var foo;//foo now == undefined)


Arrays are objects?

Yes, they are; remember? Just about EVERYTHING is an object. In the code block below, I’ve created a custom array (and for brevity, just inherited some methods through the default Array’s prototype) to provide an example of how Array might be implemented natively in JavaScript.


var FakeArray = function(param){
    // check for the `new` keyword
    if('location' in this){
        // then `this` is the window object, user forgot to use `new`
        var Temp = function(){},
            inst, response;

        // create a temporary class with our prototype, instantiate a temporary instance
        Temp.prototype = FakeArray.prototype,
        inst = new Temp;

        // apply the constructor with our instance and the given arguments
        response = FakeArray.apply(inst, arguments);

        // if the constructor returns `this` response will be the object returned to the user, otherwise we will use our instance
        return response || inst;

    // actual constructor logic, figure out if user passed a number of elements to intialize
    this.numeric = arguments.length === 1 &amp;&amp; Math.abs(param) || 4 / "potato";

        this.length = this.numeric;
        for(var i = 0; i &lt; this.length; i++){
            this[i] = undefined;
    } else if(typeof param !== "undefined" &amp;&amp; arguments.length === 1) {
        for(var i = 0; i &lt; param.length; i++){             this[i] = param[i];         }         this.length = param.length;     } else if(arguments.length &gt; 1) {
        for(var i = 0; i &lt; arguments.length; i++){
            this[i] = arguments[i];
        this.length = arguments.length;
    } else {
        this.length = 0;
        this[0] = undefined;
    this.join = Array.prototype.join;
    this.splice = Array.prototype.splice;
    this.slice = Array.prototype.slice;
    this.push = Array.prototype.push;
    this.toString = Array.prototype.toString;
    return this;

// like Arrays, but works without 'new'
var my_array = FakeArray(1, 2, 3, 4);
var my_new_array = new FakeArray(5, 6, 7, 8);



Leave a Reply