Monday, December 31, 2012

JavaScript - The Good Parts - Part 1

Introduction

JavaScript was and is still a much maligned language although it is gaining popularity. It is still dismissed as a language useful only to validate inputs in the browser. The fact is that JavaScript is a very powerful language and has features that far exceed the ones available in a traditional Object Oriented Programming Language is thus far known and appreciated only by a minority.
The name "JavaScript" gives a strong connotation that it is a lesser sibling or a subset of Java. But nothing could be further from truth.
This is the first part in the series of posts based on the book JavaScript - The Good Parts by Douglas Crockford.
BTW: This book is a must read for anybody working with JavaScript.
In this post we will cover a few basic topics in JavaScript.

Variables in JavaScript

Some facts about the variables in JavaScript
  • All variables in JavaScript are by default Global. This means any undeclared variable is a Global variable.
  • Variables declared inside a function are local to the function.

Checking Type of Variable – typeof operator

Since JavaScript is not a static typed language there is a mechanism to determine the Type of a variable at any point in time.
typeof <variable> will return one of the following values:
  1. number
  2. string
  3. boolean
  4. undefined: This indicates that no value has been assigned to the variable. This is different from null
  5. function: Note that a variable can be a function. This is possibly one of the biggest advantages of Java over the Object Oriented Languages. It is also the least understood feature given the obsession with Classes and Objects of the Object Oriented Programmers. We will see more details and power of this feature in later posts.
  6. object: Note that we can have objects in JavaScripts, making it no lesser than any other Object Oriented Languages.

Truthy and Falsy values in JavaScript

It is necessary to understand what values evaluate to a "false" and what values evaluate to "true". This is important to understand as many of the libraries that have been written in JavaScript exploit these features and without this one can get lost.
The following values are considered Falsy in JavaScript
  1. Boolean value false
  2. Object value undefined
  3. Number value 0
  4. Object value null
  5. The empty string ""
  6. The Number NaN (Not a Number)
Everything else is truthy value.
Important notes
The boolean value of a string containing the text "false" is true.
A string value "0" is true while a number value 0 is false.

The || operator

Although the || operator is used for Boolean "OR" operations it actually returns the value of the first operant which is truthy. So in essence it is not really an OR operator in the traditional sense, in that it does not return a boolean "true" or a "false", rather it returns the first value in the statement which evaluates to "true".
It is returns the last value if none of them evaluates to "true".
See the example given below:
var a;
var b = 0;
var c = false;
var d = {};
var e = 1;
var f = a || b || c || d || e;
f will be equal to d.

The && operator

Although the && operator is used for Boolean "AND" operations it returns the value of the last operant which is truthy.
var a;
var b = 0;
var c = false;
var d = {};
var e = 1;
var f = a && b && c && d && e;
var g = d && e;
f will be equal to "undefined" obtained from "a".
g will be equal to "e".
This can be used to do something as follows:
var a = node && node.name; 
instead of a verbose code like this.
var a; 
if (node) {
    a = node.name;
}
In the next post we will look at Objects in JavaScript and the associated features. Till then enjoy coding in a typeless language and if you do not wish to wait for the posts then pick up the book or listen to the topics covered in the book from horses mouth (Douglas Crockford)



No comments: