Useful Features of JavaScript That You Have Never Seen Before

JavaScript is a very versatile language. It is becoming more and more used in modern web applications, and predictions say that this trend will continue. Knowing your language is a key to success when it comes to development. In this post we will show you some cool and useful features of JavaScript that most people are unaware of.

Okay, I lied. You may actually have seen some of these before. Anyway if you have, now is a good time to refresh your memory!

== is not transitive

This one is not so obvious, but very important to know about. The check “==” is, in fact, not transitive in JavaScript.

if (('' == 0) && (0 == '0'))
    ('' == '0') ?
        alert('is transitive') : alert('is NOT transitive');

Conclusion: Always use “===” and “!==” instead of “==” and “!=”.

Count expected and received function parameters

Have you ever wondered how many arguments a specific function expects you to include? To figure this out, simply use the length-function:

function howmany(one, two, three, four, five) { }
alert(howmany.length); // 5

You can also use a similar technique to figure out how many arguments that was included:

function numberOfArguments() {
     return arguments.length;
}
alert(numberOfArguments(1,2,3,4,5)); // 5

The debugger statement

You can use the debugger statement to set programmatically breakpoints in your code. If a debugger is active, it will cause it to break.

debugger;

Extending the JavaScript language

You can extend existing JavaScript functions by using Extension methods. In this example we extend the Array class with a contains-method:

Array.prototype.contains = function(value) {  
    for (var i = 0; i < this.length; i++)
        if (this[i] == value) return true;
    return false;  
}

We can then use the extension method as follows…

var myArray = ["a", "b", "c"];
myArray.contains("c"); // true

Self-executing functions and Block scope

Since JavaScript does not have block-scope, you can emulate this behavior with self-executing functions.

(function() {
     var a = "Hello!";
     alert(a); // Hello!
})();
alert(a); // undefined

Declaring variables

It is known that these two ways of declaring variables are both working:

variableA = "hello";
var variableB = "goodbye";

Using the 2nd way, will however (unlike the 1st way) give the runtime a real chance of optimizing access to that variable. This can enhance the performance of your application if you have many declarations.

Functions are objects and can have properties

JavaScript functions are regular objects so you can actually add properties to them. This is very handy if you need to set default values that the function will use.

saySomething = function() {
  alert(saySomething.message);
};
saySomething.message = 'PixelTango is awesome';
saySomething();

The “in”-operator

Did you know that there is a “in”-operator in JavaScript? This can be used to check if a key exists in an object:

var o = { 8:2008, 9:2009, 10:2010 };
8 in o; // true, key 8 exists with a value of 2008
1 in o; // false, the key 1 does not exist in o

Use logical OR to assign default values

This one is really handy to get rid of simple null-checks before assigning values to your variables

function assignVariable(arg) {
arg = arg || 'Default value';
}

Object properties can be accessed with []

This one is maybe obvious, but it is very useful nonetheless. You know that you can access an object’s properties using “.”, right?

var obj = { coolProperty: "Hello world" };
alert(obj.coolProperty); // Hello world

…but did you know you can also use the []-syntax?

var theProperty = "coolProperty";
alert(obj[theProperty]); // Hello world

Recommended reading

If you are passionate about web development and JavaScript in general, you should check out the book JavaScript: The Good Parts.

Did you enjoy this?

Support us by sharing this article with your friends. This way we will be able to produce more content like this, and in the end we are all winners!

Comments and Feedback

PixelTango Original © 2010-11-11 | Usage

About the Author

Mickel is a web creative that is constantly looking for new challenges and ventures.

He is the founder of PixelTango, as well as a interactive web design agency. He also likes to DJ and produce music under the name Allic.

Sorry to interrupt, but...

It seems you are using AdBlock to block our advertisement (yes, 1 tiny ad). Displaying ads is essential for us to keep good quality of our posts here at PixelTango.

Please unblock, so that we can keep this site alive!