Interesting things to know about JavaScript before start using it

Interesting things to know about JavaScript before start using it

JavaScript is a flexible language, that can be used to do all kinds of crazy stuffs. It has gone a long way since its birth in 1995. It was designed specifically for electronic documents on the World Wide Web. JavaScript used to represent dynamic web documents by embedded within Hypertext Markup Language. JavaScript may look like Java, but it is not Java. As a scripting language, no doubt JavaScript is absolutely essential for any web developer. In this geek story we'll discuss about few interesting features of JavaScript that you may not know yet.

So, let's start -

Add properties to almost everything

JavaScript only has three primitive data types - Number, String, and Boolean. Everything else can have properties added to it.

var object = {}; // an object
object.prop = 'hello';
var array = []; // an array
array.prop = 'hello';
var fun = function() {}; // a function
fun.prop = 'hello';

Functions are objects

In JavaScript, functions can be treated as an object and parameter a as function. This feature allows developers to do some very powerful things like setting up event handlers with very little code.

function fun(flag, data) {
   if(flag)
    data();
}
fun(true, function() {alert('hello');}); //alerts "world"
fun(false, function() {alert('world');}); //does nothing

NaN is a Number

The NaN means "not a number" but it is a number. NaN is not considered equal to itself. In fact NaN is not equal to anything. The only way to confirm that something is NaN is via the function isNaN().

alert(typeof NaN); //Number
alert(NaN === NaN); //false

For loops iterate over property names

Java or C# languages have so-called "foreach" loops, which iterate over all the values in a collection. But, JavaScript doesn't have an equivalent loop. The closest thing, a "for in" loop. The loop gives only keys by which other values can be found, but have to perform the extra step of getting the values off of the original object.

var arr = ['apple', 'banana', 'cat'];
for(var i in arr) {
  alert(i); // 0, 1, 2
  alert(arr[i]); // 'apple', 'banana', 'cat'
}

Variable scoping

Developers avoid using global variables. But it's easy to make a mistake in Javascript because nobody's forcing you to organize your code into modules. In Javascript, all variables declared with the var keyword are scoped to their declaring function. That means if you want to make sure your variables aren't global, you should put them in a function, and then call that function.

Automatic type conversions

Like many other languages, JavaScript does some automatic conversions between types under certain circumstances.

var s = 1 + "";

It's not just for browsers

Nowadays, technologies like node.js allow JavaScript to be run outside of any browser. Today, many websites have been served by JavaScript based web servers. Not only its performance is a blast, the truly simple API attracts a lot of developers, and If you're a web application developer, you must need to learn JavaScript.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.