Some interesting facts about javascript

We all know that javascript is one of the most popular programming languages now-a-days. Javascript is a very strange language in deed. One reason is it has got syntax similar to C, C++ and Java but semantically this is not similar which makes developers confused. Another weird thing to mention is its prototype inheritance which can be achieved similarly using es6 class. Let us discuss some interesting facts about this very language.

  • Many programming languages use semi colon at the end of a statement. Javascript does so but you can also use semi colon at the beginning of a statement.
;var a = 2

The above code snippet will display 2 in the console without throwing any error!

  • In javascript you can add a number with a string. The result will be a string without any error.
var b = 5 + '9';

The above code snippet will display "59" in the console!

  • In javascript the comparison operators act really weird in many cases. Lets see some examples:
NaN == NaN // -> false
NaN === NaN // -> false
[] == true // -> false
[] === true // -> false
[] == false // -> true
[] === false // -> false
{} == {} // -> false
{} === {} // -> false
{} >= {} // -> true
{} > {} // -> false

Things got a bit messed up, right?

  • Javascript has a nice feature named Immediately Invoked Function Expression where a function can be executed just after it has been defined without being called explicitly.
(function() {
  console.log('works well');

function() {
  console.log('generates syntax error');

Here the first function works fine as it is a IIFE but the second one generates SyntaxError.

  • In javascript the difference in parenthesis position can make two functions different.
function f1() {
      grade: 'A+'
function f2() {
   return {
      grade: 'A+'
typeof f1() === typeof f2(); // -> false
  • In javascript undefined is not a reserved word although it a has a special meaning. This is the only way to determine whether a variable is undefined but the following code snippet looks quite weird.
undefined = "I am defined now!";
var c;
console.log(c == undefined); // -> false