1. JavaScript is loosely typed.
2. JavaScript is an example of a functional programming language.
3. JavaScript is interpreted.
4. JavaScript has prototype based inheritance and OO capabilities
Though JavaScript or specifically Core JavaScript is primarily used to script DOM, JavaScript can also be used as a scripting language for Java (Rhino), Adobe Flash (Action Script), Windows Script Host (WSH) and Spidermonkey(it is a interpreter for JavaScript written in C). For the remainder of this blog I'll use the term "JavaScript" to indicate its client side or DOM based avatar.
Starting with the ugly features of JavaScript -
- Variable Scoping in JavaScript is really really ugly. An implicit declaration of a variable in a function is treated as global. Worse, block scopes do not exist in JavaScript. Meaning any variable declared inside any loop or if,else condition is scoped throughout the function.
- DOM incompatibility was a serious issue years back when browser war between Netscape and IE was at its peak. With w3c standards for DOM situation has improved. But developers continue to face browser incompatibility issues with IE on one side and rest of browsers on the other. With greater adoption of IE 7 however situation seems to be improving.
- Errors made trying to woo Java developers - Inclusion of wrapper classes of primitive types such as numeric, string, boolean in JavaScript for the language to be compatible with Java was pathetic. Use of wrapper classes hampers efficiency, as wrapper classes have to be converted to their primitive types for simple operations such as concatenation and addition.
- "With" - The "with" operator in JavaScript was another attempt to woo Java developers who could not get used to JavaScript syntax of referring properties inside the object using the "this" keyword. "with" created further confusion in scoping of variables and made the code difficult to optimize.
- Null, Undefined - null and undefined both refer to different things in JavaScript. null is something that is not an object, primitive type or undefined. Whereas undefined is a variable that has been declared but not defined. The "==" operator returns true for null == undefined. Whereas "===" operator returns false. Yuk!!
- Overloaded "+" - "+" operator in JavaScript is overloaded for both concatenation of strings and addition of numeric type. Since JavaScript is loosely typed it attempts implicit casting when it encounters operators and functions. Strings "2" and "4" give numeric type 8 when following operation is performed - "2" * "4". However, "2" and "4" would give string "24" if following operation is done "2"+"4". Some people use eval to overcome this problem which makes things even worse. eval is unsafe and inefficient and should be avoided as far as possible.
- Everything apart from primitive types, null and undefined is an object. This includes functions. Functions are first class JavaScript objects. This means that functions can be passed as objects, can have properties etc. This feature is top notch.
- Every Object is a collection (like a Hash). This means every property of an object can be accessed like a hash using the key (identifier) to access it. This makes it possible to iterate over the Object using a loop. And every object can be used as a container. Even better, you can add properties to objects after it has been defined. That means you can extend classes provided to you by Core JavaScript Library, though doing so may not necessarily be a good idea.
- Literal syntax of JavaScript is beautiful. Objects go like {x:2,y:3} and arrays are defined like [[2],"hi",true].
- Use of function literals and lambda functions is perhaps what makes JavaScript a true functional language. These features put JavaScript at par with languages like Lisp, Haskell, ML etc. It makes advanced techniques like nesting functions, currying, closures possible in JavaScript. This is so cool that even though JavaScript is not OO like C++, Java it can simulate classes, private variables, getter and setter functions.
